Click here to Skip to main content
15,908,673 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Help! in C++ How to implement 2 classes share one class Pin
Prakash Nadar27-Dec-04 16:21
Prakash Nadar27-Dec-04 16:21 
GeneralRe: Help! in C++ How to implement 2 classes share one class Pin
stans8027-Dec-04 18:20
stans8027-Dec-04 18:20 
GeneralRe: Help! in C++ How to implement 2 classes share one class Pin
Prakash Nadar27-Dec-04 18:28
Prakash Nadar27-Dec-04 18:28 
GeneralRe: Help! in C++ How to implement 2 classes share one class Pin
stans8027-Dec-04 22:32
stans8027-Dec-04 22:32 
GeneralRe: Help! in C++ How to implement 2 classes share one class Pin
Prakash Nadar27-Dec-04 23:41
Prakash Nadar27-Dec-04 23:41 
GeneralCommon header Pin
Dennis Gourjii27-Dec-04 23:58
Dennis Gourjii27-Dec-04 23:58 
GeneralRe: Common header Pin
Prakash Nadar28-Dec-04 0:33
Prakash Nadar28-Dec-04 0:33 
GeneralRe: Help! in C++ How to implement 2 classes share one class Pin
Henry miller28-Dec-04 4:37
Henry miller28-Dec-04 4:37 
Sounds like you might need a factory.

class factory {
factory(int num, a *A, b *B) {
A =new a(num);
B = new b(num, a,C);
}
};

class a {
private: // THIS IS IMPORTANT!
a(...)
friend class factory
class C;
...
}

class b {
private: // AGAIN, IMPORTANT
b(...)
...
}


Note that I skiped some details, that you will need to take care of, but this should give the idea.

It occurs to me that you could do this differently with a static vector. I don't use the STL (I have to support a compiler that doesn't support modern C++), but something like this instead:

class Cfactory {
private:
static STL::vector<class c=""> Cs; // I'm not sure about this syntax! The static is critical to this though
static getSharedC(num) {
c *C;
if((C = Cs[num]) == NULL) {
C = new c(num);
Cs.add(num,C);
}
return C;
}
};

Again, I left out a lot of details, and I'm not even sure how the vector class works, but you should be able to make it work.

The last has two major problems! The first is easy to work around, but the second could be a show-stopper.

First, you need something to prevent memory leaks. Reference counting is easiest (that I know of, maybe a smart pointer would work?), something needs to make sure that c1 goes away only after both a1 and b1 is deleted.

Second, this gives you global context for all c1. You can't have different two parts of the code creating their own a1, because even though the a1 class is different, those two instance share the same c1! You need to figure out how to deal with this. I can't think of anything that I'd really trust.
QuestionHow to list the program hooking windows Pin
lekhacnhu27-Dec-04 13:49
lekhacnhu27-Dec-04 13:49 
QuestionHow to release HDC ? Pin
uus9927-Dec-04 13:47
uus9927-Dec-04 13:47 
AnswerRe: How to release HDC ? Pin
PJ Arends27-Dec-04 14:38
professionalPJ Arends27-Dec-04 14:38 
Generalintegrating a function... Pin
aaadetos27-Dec-04 12:07
aaadetos27-Dec-04 12:07 
Generalconst pointer Pin
act_x27-Dec-04 10:48
act_x27-Dec-04 10:48 
GeneralRe: const pointer Pin
Prakash Nadar27-Dec-04 18:38
Prakash Nadar27-Dec-04 18:38 
GeneralRe: const pointer Pin
Dennis Gourjii28-Dec-04 0:09
Dennis Gourjii28-Dec-04 0:09 
GeneralRe: const pointer Pin
Henry miller28-Dec-04 4:42
Henry miller28-Dec-04 4:42 
GeneralCallback Function Pin
RedDragon2k27-Dec-04 10:13
RedDragon2k27-Dec-04 10:13 
GeneralRe: Callback Function Pin
Tom Wright27-Dec-04 10:30
Tom Wright27-Dec-04 10:30 
GeneralRe: Callback Function Pin
RedDragon2k27-Dec-04 10:43
RedDragon2k27-Dec-04 10:43 
GeneralRe: Callback Function Pin
Tom Wright27-Dec-04 12:00
Tom Wright27-Dec-04 12:00 
GeneralRe: Callback Function Pin
Dennis Gourjii28-Dec-04 0:15
Dennis Gourjii28-Dec-04 0:15 
Generalpassing a struct with WM_COPYDATA Pin
Tom Wright27-Dec-04 9:14
Tom Wright27-Dec-04 9:14 
GeneralRe: passing a struct with WM_COPYDATA Pin
PJ Arends27-Dec-04 11:01
professionalPJ Arends27-Dec-04 11:01 
GeneralRe: passing a struct with WM_COPYDATA Pin
Tom Wright27-Dec-04 11:33
Tom Wright27-Dec-04 11:33 
GeneralRe: passing a struct with WM_COPYDATA Pin
PJ Arends27-Dec-04 11:53
professionalPJ Arends27-Dec-04 11:53 

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.