class in another class
I have a question, how do I define two class class1, class2 such that class2 is a member of class1 and class1 is a member of class2? If I use code like the below, I get error.
class class1{
class2 aa;
};
class class2{
class1 bb;
};
You can do this by declaring the class above the one you want to use it in. You must use a pointer to that class though.
Here's an example:
class class2; //forward declaration of class2
class class1
{
class2 *a //must be pointer!
};
class class2
{
class1 b; //class1 as usual
};
Hope this helps.
Andrew
Edited by - acraig on 3/23/00 5:16:11 PM
Here's an example:
class class2; //forward declaration of class2
class class1
{
class2 *a //must be pointer!
};
class class2
{
class1 b; //class1 as usual
};
Hope this helps.
Andrew
Edited by - acraig on 3/23/00 5:16:11 PM
CarlFGauss: Actually that won''t work; look at this example:
class1 myObject;
What happens there, is that myObject constructs a class1 object, which constructs a class2 object as a member, which in turn constructs a class1 object as its member, which in turn... it''ll repeat like that forever.
When you run into a situation like that, it''s usually better design to make class1 construct a class2 object, and pass a pointer to this as an argument to the class2 object''s constructor. In other words, make one of them control the other.
Good Luck!
- null_pointer
class1 myObject;
What happens there, is that myObject constructs a class1 object, which constructs a class2 object as a member, which in turn constructs a class1 object as its member, which in turn... it''ll repeat like that forever.
When you run into a situation like that, it''s usually better design to make class1 construct a class2 object, and pass a pointer to this as an argument to the class2 object''s constructor. In other words, make one of them control the other.
Good Luck!
- null_pointer
null_pointer: That is the reason acraig said that one of the objects had to be a point. You are right, though, it will still be pretty tricky to avoid this infinite allocation (although, with the pointer it is possible)
I have to ask, though, if class1 and class2 are so closely related, why not just combine them? Just a thought since you will probably want to rethink your design strategy since the one you have now will be pretty tough to work with (unless you are trying to develop some kind of complicated tree-like structure)
I have to ask, though, if class1 and class2 are so closely related, why not just combine them? Just a thought since you will probably want to rethink your design strategy since the one you have now will be pretty tough to work with (unless you are trying to develop some kind of complicated tree-like structure)
This is always a very tricky situation, especially when defining the classes in different header files. Not recommended that you go this route, but sometimes it is needed.
chippydip: No, the only reason acraig said about using a pointer was so that each class could use the other. There is a big difference: he said nothing about when the memory was allocated. In other words, he was pointing out that you can't do this:
class class1;
class class2;
class class1
{
class2 aa;
};
class class2
{
class 1 bb;
};
His explanation was simply solving the syntactical (sp?) problem.
The real reason for the syntactical problem is that the compiler does not know the size of class2 before it is declared. However, it does know the size of a class1 pointer, because it knows the memory addressing scheme that the program is compiled for (pointers are typically 32-bit -- 4 bytes -- nowadays).
Think of it -- if that example did work syntaxically (sp?), the compiler would crash when trying to figure out how much memory the classes would use when they are created. For example, the memory requirement of class1 would be:
class1_mem = class2_mem + class1_mem + class2_mem... // and on and on and on...
Otherwise, the compiler would be able to handle the problem easily. You're right though, it does indicate flawed logic. You could do something like this, however:
class File
{
public:
class Pointer // a 64-bit value
{
public:
unsigned long nHighByte;
long nLowByte;
bool bValid; // indicates a valid operation
};
public:
File();
virtual ~File();
void SetPointer(File:: Pointer);
File:: Pointer GetPointer();
};
In that way, the File object controls or manipulates the File:: Pointer object. As I said before, though, if you have two separate classes, and they both need to call the member functions of the other, then you must use pointers to each other and decide which one gets allocated first.
Good Luck!
- null_pointer
Edited by - null_pointer on 3/24/00 7:10:24 AM
class class1;
class class2;
class class1
{
class2 aa;
};
class class2
{
class 1 bb;
};
His explanation was simply solving the syntactical (sp?) problem.
The real reason for the syntactical problem is that the compiler does not know the size of class2 before it is declared. However, it does know the size of a class1 pointer, because it knows the memory addressing scheme that the program is compiled for (pointers are typically 32-bit -- 4 bytes -- nowadays).
Think of it -- if that example did work syntaxically (sp?), the compiler would crash when trying to figure out how much memory the classes would use when they are created. For example, the memory requirement of class1 would be:
class1_mem = class2_mem + class1_mem + class2_mem... // and on and on and on...
Otherwise, the compiler would be able to handle the problem easily. You're right though, it does indicate flawed logic. You could do something like this, however:
class File
{
public:
class Pointer // a 64-bit value
{
public:
unsigned long nHighByte;
long nLowByte;
bool bValid; // indicates a valid operation
};
public:
File();
virtual ~File();
void SetPointer(File:: Pointer);
File:: Pointer GetPointer();
};
In that way, the File object controls or manipulates the File:: Pointer object. As I said before, though, if you have two separate classes, and they both need to call the member functions of the other, then you must use pointers to each other and decide which one gets allocated first.
Good Luck!
- null_pointer
Edited by - null_pointer on 3/24/00 7:10:24 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement