CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2009
    Posts
    30

    What does this constructor definition mean?

    Hello. I am learning C++. Could anybody please let me know what the following code means? CMotion is a class name. Not sure if FrameArray is a class or a function name. Thanks.

    CMotion::CMotion(void) : FrameArray()
    {

    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What does this constructor definition mean?

    FrameArray will be a member of the CMotion class. That simply means that the member should be default-constructed. Since that's what would normally happen anyway, putting it there is redundant, but doesn't hurt anything.

    If would be meaningful if FrameArray's class didn't have a default-constructor, so you had do do something like
    Code:
    CMotion::CMotion(void) : FrameArray(0)
    in order to compile.

  3. #3
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: What does this constructor definition mean?

    I am learning C++. Could anybody please let me know what the following code means?
    It can be a defaut initialization of a member variable. It can be an explicit call of the base class default constructor also.

  4. #4
    Join Date
    Apr 2009
    Posts
    30

    Re: What does this constructor definition mean?

    Thank you for your reply. Do you mean FrameArray is a "member function" of the CMotion class?
    I just checked the corresponding .h file (written by others) and found the following:

    class CMotion : private FrameArray, public CTManager

    It seems that class CMotion is inherited from class CTManager and (class? function?) FrameArray. I am confused...

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What does this constructor definition mean?

    Quote Originally Posted by hajimeml View Post
    Thank you for your reply. Do you mean FrameArray is a "member function" of the CMotion class?
    That's why you post all of the relevant code. FrameArray() could have been a member variable, a parent class, or a macro that does something.
    I just checked the corresponding .h file (written by others) and found the following:

    class CMotion : private FrameArray, public CTManager
    Then obviously, FrameArray is a parent class.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Mar 2006
    Location
    Inida
    Posts
    119

    Re: What does this constructor definition mean?

    C++ supports Multiple inheritence.

    Code:
     
    class CMotion : private FrameArray, public CTManager
    The above syntax says that CMotion is a class derived from FrameArray with private acess and from CTManager with public access.

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

    Re: What does this constructor definition mean?

    Quote Originally Posted by ixSci
    It can be a defaut initialization of a member variable.
    Strictly speaking, I believe it is considered value initialisation rather than default initialisation, though there would only be a possibility of a difference in effect if FrameArray has no user declared constructors.
    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

  8. #8
    Join Date
    Apr 2009
    Posts
    30

    Re: What does this constructor definition mean?

    So, could you please tell me what the constructor in the original post do/mean? It seems that it does nothing. I guess what confuses me is that there is a () after the base class name FrameArray. Sorry I did not post relevant code at the beginning to cause the confusion. I found the following in related files:

    typedef vector<CFrame> FrameArray

    and

    class CFrame
    {
    private:
    Mat3x3d mat_data;
    Vec3d vec_data;
    :
    :
    }

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What does this constructor definition mean?

    Whoa. Is someone trying to derive from std::vector? Isn't that a no-no?

  10. #10
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: What does this constructor definition mean?

    Quote Originally Posted by Lindley View Post
    Whoa. Is someone trying to derive from std::vector? Isn't that a no-no?
    As with most things, it depends.

    Generally speaking, you should avoid (publicly) deriving from anything that doesn't have a virtual destructor because you risk running into undefined behavior if someone tries to use your type in a polymorphic fashion.

    Private inheritance (in combination with appropriate using-directives), on the other hand, will allow you to extend the functionality of STL facilities without having to write wrapper functions as would be required with composition. Assuming that you don't need to modify their behavior, that is.
    Last edited by Plasmator; April 21st, 2009 at 01:07 AM.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What does this constructor definition mean?

    Hmm, good to know.

  12. #12
    Join Date
    Apr 2009
    Posts
    30

    Re: What does this constructor definition mean?

    Quote Originally Posted by Lindley View Post
    FrameArray will be a member of the CMotion class. That simply means that the member should be default-constructed. Since that's what would normally happen anyway, putting it there is redundant, but doesn't hurt anything.

    If would be meaningful if FrameArray's class didn't have a default-constructor, so you had do do something like
    Code:
    CMotion::CMotion(void) : FrameArray(0)
    in order to compile.
    So, what does the () after the class name FrameArray mean?
    As there is nothing in between {}, can I assume that the code in the original post does nothing?

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What does this constructor definition mean?

    Quote Originally Posted by hajimeml View Post
    So, what does the () after the class name FrameArray mean?
    Are you just randomly cherry-picking C++ syntax to look at, or are you learning C++ in a systematic, formal way? You cannot learn C++ by just looking at random code, and asking "what does this do"?

    Are you using C++ books? If so, what does the book say about the member initialization list (which is what this entire discussion is about)?

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Apr 2009
    Posts
    30

    Re: What does this constructor definition mean?

    I am very sorry. I try to modify a complex program written by former students. I have C++ How to Program and C++ Primer Plus. As far as I know, the member initialization list is for initializing the data members (i.e. variables) only. In this case, FrameArray is one of the base classes of CMotion, not a variable. So, I don't know that the code in the original post means. Do you mean we can initialize class also by putting it in the member initialization list?

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What does this constructor definition mean?

    You can do a partial initialization by calling one of the base class's constructors, yes. Again, since the default constructor would have been called anyway, I don't think having it there actually helps any.

Page 1 of 2 12 LastLast

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