Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Doubt about container
Doubt about container [message #52490] Tue, 08 October 2019 10:19 Go to next message
koldo is currently offline  koldo
Messages: 3004
Registered: August 2008
Ultimate Member
I have a doubt about how to keep a class... probably it is extremely simple to solve Smile
This is the story:

- A class (A) has to keep a copy of other class (B)
- A can keep:
-- Case 1. a pointer to an instance of B created with new() by A
-- Case 2. a pointer to an instance of B handled in other place
- When A is destroyed
-- Case 1. B is destroyed in A destructor with a delete
-- Case 2. B is not destroyed. The owner has to destroy it

The problem of Case 2 it is that it is possible that B owner destroys B before A is destroyed... so if A is used in between, it can cause problems.

What is the proper way to do it?


Best regards
Iñaki
Re: Doubt about container [message #52493 is a reply to message #52490] Wed, 09 October 2019 11:18 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12050
Registered: November 2005
Ultimate Member
koldo wrote on Tue, 08 October 2019 10:19
I have a doubt about how to keep a class... probably it is extremely simple to solve Smile
This is the story:

- A class (A) has to keep a copy of other class (B)
- A can keep:
-- Case 1. a pointer to an instance of B created with new() by A
-- Case 2. a pointer to an instance of B handled in other place
- When A is destroyed
-- Case 1. B is destroyed in A destructor with a delete
-- Case 2. B is not destroyed. The owner has to destroy it

The problem of Case 2 it is that it is possible that B owner destroys B before A is destroyed... so if A is used in between, it can cause problems.

What is the proper way to do it?


It would be more helpful to explain the situation with the code. That said, if the task is to "A class (A) has to keep a copy of other class (B)", then what is wrong with

class A {
   B b;
}


?
Re: Doubt about container [message #52494 is a reply to message #52493] Thu, 10 October 2019 08:49 Go to previous message
koldo is currently offline  koldo
Messages: 3004
Registered: August 2008
Ultimate Member
Laughing Laughing

The examples must be simple, but the simplicity of the examples should not be misunderstood.

As I expected, the answer was terribly easy: Ptr. From Reference/Ptr:

struct Foo : Pte<Foo> {String text;};

CONSOLE_APP_MAIN {
	Ptr<Foo> ptr;
	{
		Foo foo;
		foo.text = "Text";
		ptr = &foo;
		Cout() << (void*)~ptr << " -> " << ptr->text << "\n";
	}
	Cout() << (void*)~ptr << "\n"; // foo was destroyed but this is totally valid, as ptr == 0
}


Best regards
Iñaki
Previous Topic: BETA status handling
Next Topic: S_type and ValueMap
Goto Forum:
  


Current Time: Thu Oct 17 13:27:27 CEST 2019

Total time taken to generate the page: 0.01183 seconds