Click here to Skip to main content
15,899,754 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: which design is better to wrap another class instance Pin
George_George12-Apr-08 1:23
George_George12-Apr-08 1:23 
GeneralRe: which design is better to wrap another class instance Pin
CPallini12-Apr-08 1:48
mveCPallini12-Apr-08 1:48 
GeneralRe: which design is better to wrap another class instance Pin
George_George12-Apr-08 1:56
George_George12-Apr-08 1:56 
GeneralRe: which design is better to wrap another class instance Pin
CPallini12-Apr-08 2:33
mveCPallini12-Apr-08 2:33 
GeneralRe: which design is better to wrap another class instance Pin
George_George12-Apr-08 3:46
George_George12-Apr-08 3:46 
GeneralRe: which design is better to wrap another class instance Pin
Blake Miller14-Apr-08 5:56
Blake Miller14-Apr-08 5:56 
GeneralRe: which design is better to wrap another class instance Pin
George_George14-Apr-08 22:41
George_George14-Apr-08 22:41 
GeneralRe: which design is better to wrap another class instance Pin
Blake Miller15-Apr-08 7:03
Blake Miller15-Apr-08 7:03 
Suppose I have a list of instances of objects of type "Class A".
Now I want to refer to those Class A objects by specific instance from another class, let's say Class B.

Traditionally, you would have placed a pointer or some reference to the Class A instance within Class B:

class B<br />
{<br />
   ...<br />
   class A* m_pA;<br />
   ...<br />
};


But instead, if you give every instance of class A a 'cookie', such as a unique 32-bit DWORD value:

class A{<br />
   ...<br />
   DWORD m_Cookie;<br />
   ...<br />
};


And then you palce all the instances of Class A into a map, mapping the cookie to the pointer to Class A (So now the pointer to class A is in exactly one place).

Then when you need class B to refer to a specific instance of Class A, instead of using the pointer to Class A, you store its cookie value:

class B
{
...
DWORD m_CookieToA;
...
};

Then when you need to get to a Class A instance from a member function of Class B, you can use the map to look for the cookie for the related Class A. If the map gives you back a value, then you get back a good pointer to class A, and if not, then there is no Class A with that cookie in existence.

If the lifetime of Class A objects will outlive Class B objects, this is fine either way - pointer or cookie.

Otherwise, you might want to provide some synchronization on the map (unless your program is single threaded) so things are not removed from the Class A map while a Class B member function retains or is using a pointer to a Class A object. Otherwise, you are free to delete Class A objects at will, without trying to patch up your Class B objects which might have stored pointers to the Class A instances.

So, you can ask yourself, if it is more important not to crash or to have performance, as the other member mentioned. This technique is not as 'quick' as storing the pointer to Class A within Class B, as far as the quickest reference to Class A from within a Class B member function (you have to look up the cookie in the map each time to fetch the temporary pointer to Class A).

If you wanted to get fancy, you could use a multimap for Class A, and map Class A address, Class A cookie, and the number of references to class A in the map. The reference count would be udpated each time you assigned a cookie to an A to a B. That way, you would know, as yuo were about to delete a Class A instance, if there were any outstanding Class B referring to it.
GeneralRe: which design is better to wrap another class instance Pin
George_George16-Apr-08 0:52
George_George16-Apr-08 0:52 
Generalre-entrancy pattern issue setbacks Pin
George_George11-Apr-08 22:24
George_George11-Apr-08 22:24 
QuestionHow can remove button of Property Sheet? Pin
Le@rner11-Apr-08 21:44
Le@rner11-Apr-08 21:44 
AnswerRe: How can remove button of Property Sheet? Pin
Blake Miller14-Apr-08 5:58
Blake Miller14-Apr-08 5:58 
Generalcall one dialog box from another in sdk Pin
Pankaj Kothawade11-Apr-08 21:00
Pankaj Kothawade11-Apr-08 21:00 
AnswerRe: call one dialog box from another in sdk Pin
Hamid_RT12-Apr-08 7:19
Hamid_RT12-Apr-08 7:19 
GeneralMenu On RButtonDown of Mouse. Pin
Le@rner11-Apr-08 20:39
Le@rner11-Apr-08 20:39 
GeneralRe: Menu On RButtonDown of Mouse. Pin
enhzflep12-Apr-08 7:14
enhzflep12-Apr-08 7:14 
GeneralRe: Menu On RButtonDown of Mouse. Pin
Hamid_RT12-Apr-08 7:19
Hamid_RT12-Apr-08 7:19 
Generalfiles present in folder Pin
neha.agarwal2711-Apr-08 19:41
neha.agarwal2711-Apr-08 19:41 
GeneralRe: files present in folder Pin
CPallini11-Apr-08 21:34
mveCPallini11-Apr-08 21:34 
GeneralUnlock Process Pin
john563211-Apr-08 17:58
john563211-Apr-08 17:58 
GeneralRe: Unlock Process Pin
Schehaider_Aymen11-Apr-08 23:11
Schehaider_Aymen11-Apr-08 23:11 
GeneralMIME Spec Pin
Bram van Kampen11-Apr-08 11:46
Bram van Kampen11-Apr-08 11:46 
GeneralPrevent active controls under child window to overwhelm it Pin
428811-Apr-08 8:02
428811-Apr-08 8:02 
GeneralRe: Prevent active controls under child window to overwhelm it Pin
Mark Salsbery11-Apr-08 8:20
Mark Salsbery11-Apr-08 8:20 
GeneralRe: Prevent active controls under child window to overwhelm it Pin
428811-Apr-08 9:22
428811-Apr-08 9:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.