Click here to Skip to main content
15,914,379 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionproblems on mutual class declaration introduction between classes Pin
cy163@hotmail.com6-Nov-06 18:37
cy163@hotmail.com6-Nov-06 18:37 
AnswerRe: problems on mutual class declaration introduction between classes [modified] Pin
Steve Echols6-Nov-06 18:51
Steve Echols6-Nov-06 18:51 
GeneralRe: problems on mutual class declaration introduction between classes Pin
Cedric Moonen6-Nov-06 20:25
Cedric Moonen6-Nov-06 20:25 
GeneralRe: problems on mutual class declaration introduction between classes Pin
Steve Echols6-Nov-06 20:29
Steve Echols6-Nov-06 20:29 
GeneralRe: problems on mutual class declaration introduction between classes Pin
cy163@hotmail.com7-Nov-06 2:25
cy163@hotmail.com7-Nov-06 2:25 
GeneralRe: problems on mutual class declaration introduction between classes Pin
Cedric Moonen7-Nov-06 2:42
Cedric Moonen7-Nov-06 2:42 
GeneralRe: problems on mutual class declaration introduction between classes [modified] Pin
cy163@hotmail.com7-Nov-06 12:15
cy163@hotmail.com7-Nov-06 12:15 
GeneralRe: problems on mutual class declaration introduction between classes Pin
Cedric Moonen7-Nov-06 20:23
Cedric Moonen7-Nov-06 20:23 
Yes, because in that case an integer is fully defined (you know its size and everything). In your case, if Class_A is declared before Class_B in the file and if it contains a Class_B has member, this Class_B has not been fully defined yet, so it is impossible for the compiler to create the Class_A.

Think of it as my example: try to put a box 1 inside another box 2 and then box 2 inside box 1. This is the same problem here. In the example with your integer, you simply put box 1 (the integer) into box 2 (your class) but your integer doesn't contain your class.

Another example: if you declared Class_A before Class_B in your example and if Class_A does't contain Class_B, then it would be ok:

// This code will compile without problem

Class_A
{
public:
   Class_A();
   ~Class_A();

   int Test;
};

Class_B
{
public:
    Class_B();
    ~Class_B();

    Class_A AnotherTest;
};


This will work because Class_A is fully defined when you use it in Class_B. Now, if you want to use Class_B inside Class_A you cannot do that directly (there is noway you can fully define both classes if they contain each other). The workaround is to use a pointer inside to Class_B inside Class_A: A pointer is always 'known' (its size is always 4 bytes). And you need to use forward declaration of Class_B (it is more or less the same as function prototype but for classes):


// This code will compile without problem

class Class_B;   // Forward declaration of Class_B

Class_A
{
public:
   Class_A();
   ~Class_A();

   int Test;
   Class_B* pB;   // We know that Class_B exists (due to the forward declaration) but it will be defined later. All we need to know is that it exists.
};

Class_B
{
public:
    Class_B();
    ~Class_B();

    Class_A AnotherTest;
};


Is it clearer now ?


Cédric Moonen
Software developer

Charting control [Updated - v1.1]

GeneralRe: problems on mutual class declaration introduction between classes Pin
cy163@hotmail.com8-Nov-06 0:52
cy163@hotmail.com8-Nov-06 0:52 
GeneralRe: problems on mutual class declaration introduction between classes [modified] Pin
cy163@hotmail.com7-Nov-06 2:19
cy163@hotmail.com7-Nov-06 2:19 
QuestionCListCtrl Pin
kk.tvm6-Nov-06 18:18
kk.tvm6-Nov-06 18:18 
AnswerRe: CListCtrl Pin
prasad_som6-Nov-06 18:36
prasad_som6-Nov-06 18:36 
AnswerRe: CListCtrl Pin
Nibu babu thomas6-Nov-06 18:43
Nibu babu thomas6-Nov-06 18:43 
AnswerRe: CListCtrl Pin
Hamid_RT7-Nov-06 4:20
Hamid_RT7-Nov-06 4:20 
QuestionHow to get IHTMLElement using elementFromPoint in Frame? Pin
31415926536-Nov-06 18:09
31415926536-Nov-06 18:09 
QuestionHow to highlight a Check box if no caption Pin
vikas amin6-Nov-06 18:07
vikas amin6-Nov-06 18:07 
AnswerRe: How to highlight a Check box if no caption Pin
Hamid_RT6-Nov-06 20:18
Hamid_RT6-Nov-06 20:18 
GeneralRe: How to highlight a Check box if no caption Pin
vikas amin6-Nov-06 21:28
vikas amin6-Nov-06 21:28 
GeneralRe: How to highlight a Check box if no caption Pin
Hamid_RT7-Nov-06 4:31
Hamid_RT7-Nov-06 4:31 
AnswerRe: How to highlight a Check box if no caption Pin
Niklas L6-Nov-06 20:54
Niklas L6-Nov-06 20:54 
GeneralRe: How to highlight a Check box if no caption Pin
Niklas L6-Nov-06 21:33
Niklas L6-Nov-06 21:33 
AnswerRe: How to highlight a Check box if no caption Pin
Blake Miller7-Nov-06 3:48
Blake Miller7-Nov-06 3:48 
QuestionInserting Data in database by Atl consumer wizard [modified] Pin
With_problem6-Nov-06 16:26
With_problem6-Nov-06 16:26 
Questionwrap/hide string references Pin
Benlahrech .Dj6-Nov-06 13:39
Benlahrech .Dj6-Nov-06 13:39 
AnswerRe: wrap/hide string references Pin
Jörgen Sigvardsson6-Nov-06 14:32
Jörgen Sigvardsson6-Nov-06 14:32 

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.