|
-
August 5th, 2009, 06:37 PM
#1
Instance of an object within class definition
I want to have an instance of a class used within its own class definition. Such as:
class A{
private:
A a;
public:
A GetA(){return a;}
};
This is a simple thing to do in java with its mighty multipass compiling, but how could I achieve this in C++?
-
August 5th, 2009, 07:47 PM
#2
Re: Instance of an object within class definition
First, it's not because of the multipass compilation so much as the means by which objects are created in Java.
In C++, this is done with pointers. I'd suggest smart pointers (they operate something like Java's use of variables).
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
August 5th, 2009, 08:48 PM
#3
Re: Instance of an object within class definition
First of all thanks for bringing up this question, I didn't even realize this wasn't possible.
As Jvene suggested, I was so used to declaring "a" as a pointer to the class "A" in such cases.
Question
-----------
Just wonder why C++ permits the declaration of a pointer to the same class while not an object of the class.
-
August 5th, 2009, 09:20 PM
#4
Re: Instance of an object within class definition
Ah yes, good point. I've actually realised that this isn't the issue that I'm having. I have two classes, each in their own header files. A uses B in its definition and B wants to use A in its definition. It seems the resolution for this is to have a dummy B class defined before A. So nevermind, thanks for your help.
-
August 5th, 2009, 09:22 PM
#5
Re: Instance of an object within class definition
because a pointer has a fixed size, 32 bits on a 32 bit machine and 64 bits on a 64 bit machine, whereas a full blown object has indeterminate size until the full declaration is seen.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
August 5th, 2009, 09:26 PM
#6
Re: Instance of an object within class definition
 Originally Posted by RedUnderTheBed
...I have two classes, each in their own header files. A uses B in its definition and B wants to use A in its definition. It seems the resolution for this is to have a dummy B class defined before A.
Look up forward declaration, here, here and here are a good start.
-
August 5th, 2009, 11:24 PM
#7
Re: Instance of an object within class definition
 Originally Posted by Russco
because a pointer has a fixed size, 32 bits on a 32 bit machine and 64 bits on a 64 bit machine, whereas a full blown object has indeterminate size until the full declaration is seen.
Thanks Russco, I didn't know that. I think I understand better.
 Originally Posted by STLDude
Look up forward declaration, here, here and here are a good start.
I don't think forward declaration might help in this case, correct me if I am wrong,
Forward declaration wouldn't solve the problem of incomplete type,
As Russco stated Objects would require the complete class specification, and for pointers forward declaration would be sufficient.
I have given below an example:
Code:
class ClassB;
class ClassA
{
public:
ClassB objB;
};
class ClassB
{
public:
ClassA objA;
};
int main()
{
return(0);
}
Compilation Error:
Code:
test.cpp:6: error: field 'objB' has incomplete type
Last edited by Muthuveerappan; August 5th, 2009 at 11:48 PM.
Reason: adding error message
-
August 6th, 2009, 12:07 AM
#8
Re: Instance of an object within class definition
 Originally Posted by Muthuveerappan
I don't think forward declaration might help in this case, correct me if I am wrong,
Forward declaration wouldn't solve the problem of incomplete type,
As Russco stated Objects would require the complete class specification, and for pointers forward declaration would be sufficient.
You're right. Plus even if this was allowed, its meaning would be rather "interesting": a class A object owns a class A object which owns a class A object which...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|