CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Posts
    4

    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++?

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    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).

  3. #3
    Join Date
    Feb 2009
    Posts
    326

    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.

  4. #4
    Join Date
    Jun 2009
    Posts
    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.

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  6. #6
    Join Date
    May 2007
    Posts
    811

    Re: Instance of an object within class definition

    Quote Originally Posted by RedUnderTheBed View Post
    ...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.

  7. #7
    Join Date
    Feb 2009
    Posts
    326

    Re: Instance of an object within class definition

    Quote Originally Posted by Russco View Post
    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.

    Quote Originally Posted by STLDude View Post
    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

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Instance of an object within class definition

    Quote 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...
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured