Incomplet Type
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Incomplet Type

  1. #1
    Join Date
    Feb 2009
    Posts
    326

    Incomplet Type

    Hi,

    I have a few questions on incomplete types, I have tried 6 different scenarios.

    scenarios:
    1) In the ClassA, declare an object of ClassA as member of the class - incomplete type
    2) In the ClassA, declare an object of ClassA inside a member function - compiles ok
    3) In the ClassA, declare an object of ClassA as static as member of the class - compiles ok
    4) In the ClassA, declare an object of ClassA as static nside a member function - compiles ok
    5) In the ClassA, declare a pointer of ClassA as member of the class - compiles ok
    6) The ClassB is forward declared, and an object of ClassB is declared - incomplete type

    My Understanding- Pls Confirm
    -------------------------------------
    My understanding is that for an object to be declared the class must be fully defined (spec must be available). Therefore 1 and 6 are incomplete.

    My Assumption - Pls Confirm
    ------------------------------------
    Anything can be inside the implementation of the members of the class. As long the class spec is available,
    there would be no problem. That is the reason why scenrios 2 and 4 are valid

    Questions:
    ----------
    1) scenario 3 - How is possible to declare a static object of ClassA as a member of ClassA ?
    2) scenario 5 - How is it possible to declare the pointer ptr inside the ClassA ?
    - Doesn't it need to know the size of the ClasssA to perform pointer arithmetic ?

    Code:
    class ClassA
    {
        public:
            void f1();
            void f2();
        
            //ClassA objA1;         //scenario 1 - incomplete type
            static ClassA objA3;    //scenario 3 - compiles ok - Can't understand why this is allowed
            ClassA *ptr;            //scenario 5 - compiles ok - Can't understand why this is allowed
    };
    
    class ClassB;
    
    
    void ClassA :: f1()
    {
        ClassA objA2;               //scenario 2 - compiles ok
    }
    
    void ClassA :: f2()
    {
        static ClassA objA4;        //scenario 4 - compiles ok
    }
    
    int main()
    {
        ClassA objA;
        //ClassB objB;              //scenario 6 - incomplete type
    
        return(0);
    }
    Thanks,
    Muthu

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Incomplet Type

    Quote Originally Posted by Muthuveerappan View Post
    1) scenario 3 - How is possible to declare a static object of ClassA as a member of ClassA ?
    Because the static object is only declared, there is no memory allocated for it. You also need to define the object (outside of the class definition/in an implementation file) which is when the full definition of the class must be available.
    Quote Originally Posted by Muthuveerappan View Post
    2) scenario 5 - How is it possible to declare the pointer ptr inside the ClassA ?
    - Doesn't it need to know the size of the ClasssA to perform pointer arithmetic ?
    No, a pointer is still a pointer and allocates a fixed amount of memory. That's all that the compiler needs for the class definition. Naturally, any function that would make use of the pointer (e.g. allocating memory for a new object using new) would need the full definition of the class.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Incomplet Type

    NB: Your test cases for scenario 2 and 4 are wrong as class A is already defined at this point in the source.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    Re: Incomplet Type

    Thank you so much Treuss

    My understanding after your explanation
    -------------------------------------------------
    Scenario 3 - That explains why we get a linker error, when the static member is accessed and it is not defined outside the class (like you rightly pointed out)

    Scenario 5 - I suppose the size of any pointer would be the same irrespective of the object it points to, like you said before it points to an object or any pointer arithmetic is done the class needs to defined.

    Question
    ------------
    Scenario 2 and 4 - Did you mean, it needs to be inline functions (shown in the code below) to serve as possible scenarios ? It still works and is the reason because they are implementations anyhow even if they are made inline ?


    Code:
    class ClassA
    {
        public:
            void f1() { ClassA objA2; }         //scenario 2 - compiles ok
            void f2() { static ClassA objA4; }  //scenario 4 - compiles ok
        
            //ClassA objA1;         //scenario 1 - incomplete type
            static ClassA objA3;    //scenario 3 - compiles ok - Memory is not allocated here
            ClassA *ptr;            //scenario 5 - compiles ok - Memory for Pointer is allocated however still
                                    //                           needs class definition for it to point to an object
                                    //                           or perform pointer arithmetic
    };
    
    ClassA ClassA :: objA3; //This is where memory is allocated and this is the definition of the objA3
    
    class ClassB;
    
    int main()
    {
        ClassA objA;
        //ClassB objB;              //scenario 6 - incomplete type
    
        ClassA :: objA3 = ClassA();
    
    
        return(0);
    }

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: Incomplet Type

    Quote Originally Posted by Muthuveerappan View Post
    6) The ClassB is forward declared, and an object of ClassB is declared - incomplete type
    Not exactly. Consider
    Code:
    class B;
    extern B b;
    class B {};
    B b;
    int main()
    {
        B bb = b;
    }
    Object b is declared before class B is defined. So it should be
    6) The ClassB is forward declared, and an object of ClassB is defined - incomplete type
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Feb 2009
    Posts
    326

    Re: Incomplet Type

    Thanks for that D_Drmmr

    I suppose I used the term "object declaration" very loosely. You are right, thanks for that.

  7. #7
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Incomplet Type

    Quote Originally Posted by Muthuveerappan View Post
    Question
    ------------
    Scenario 2 and 4 - Did you mean, it needs to be inline functions (shown in the code below) to serve as possible scenarios ? It still works and is the reason because they are implementations anyhow even if they are made inline ?
    Well, if they are inlined they are not really translated until they are called (because the code should be inlined whereever it is called).

    Apart from that, function implementations should be translated only after the class definition is complete. Think of the following example:
    Code:
    class A {
    public:
      A() : i(0) {}
      int getI() { return i; }
    private:
      int i;
    }
    The implementation of the constructor and the accessor function use the member variable i before it is declared.This should make it clear, that the compiler must first finish reading the class definition before devoting time to the method definitions.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  8. #8
    Join Date
    Feb 2009
    Posts
    326

    Re: Incomplet Type

    Thanks Truess, I think I understand better.

    For a long time, couldn't understand the reason why "incomplete type" error was thrown in certain scenarios.

  9. #9
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Incomplet Type

    2) scenario 5 - How is it possible to declare the pointer ptr inside the ClassA ?
    - Doesn't it need to know the size of the ClasssA to perform pointer arithmetic ?
    Pointer arithmetic only applies if you are dealing with arrays correct? In that case the actual pointer arithmetic would be one in a function defintion not within the declaration. Therefore the dependent class should have the header inclusion within the implementation file. By the way, that is optional. You can still include headers in other headers. The reason that forward declarations are used is to avoid unnecessary compile time dependencies. In other words changing a header file should result in as few recompilations as possible within the project. The information will still be known at compile time but will be known only to those files that absolutely must have the information in order to compile.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center