Initializing base class members in member initializer list
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

Thread: Initializing base class members in member initializer list

  1. #1
    Join Date
    Aug 2006
    Posts
    508

    Initializing base class members in member initializer list

    In my real world problem I am not using CRect/RECT pair but I am using these to explain the problem at hand which is identical to this. That is I am deriving a class from a struct and want to initialize the derived class properly.
    Code:
    class CMyRect : public RECT
    {
    public:
        CMyRect();
        CMyRect(int l, int t, int r, int b);
        ~CMyRect(void);
    };
    Now in the member initializer list, I was suprised that I cann't access the base class/struct members as in:
    Code:
    CMyRect::CMyRect(int l, int t, int r, int b) :
    RECT::left(l), RECT::top(t), RECT::right(r), RECT::bottom(b) // Not OK!
    {
        RECT::left = l; // OK
    }
    I looked into MFC code how CRect c'tor works and it also initialize it in constructor and not in member initializer list.

    Why can't we access base class members in member initializer list as they say member initializer list is preferred way to initialize? Is there a way around it?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Initializing base class members in member initializer list

    What is the definition of RECT? If RECT has an appropriate constructor, just write:
    Code:
    CMyRect::CMyRect(int l, int t, int r, int b) : RECT(l, t, r, b) {}
    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

  3. #3
    Join Date
    Aug 2006
    Posts
    508

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by laserlight View Post
    What is the definition of RECT? If RECT has an appropriate constructor, just write:
    Code:
    CMyRect::CMyRect(int l, int t, int r, int b) : RECT(l, t, r, b) {}
    I think that explains it because RECT is structure and has not default constructor. It is the same RECT as defiend in MFC.

    But in my case I have const members in base structure too, can I initialize them in c'tor of derived class? As far as I know constant members can only be initialized in member initializer list.

    I think the error message that I get is confusing though, it should complain no c'tor available for base class but it says this:
    Code:
    error C2039: 'left' : is not a member of 'tagRECT'

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by zspirit
    But in my case I have const members in base structure too, can I initialize them in c'tor of derived class? As far as I know constant members can only be initialized in member initializer list.
    Why not just provide a constructor for the base class? The constructor initialisation list is for initialising bases and members, not the members of bases.
    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

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by zspirit View Post
    It is the same RECT as defined in MFC.
    Is it the actual MFC RECT or your own that is similar to it. The MFC RECT
    does not have a virtual destructor.

  6. #6
    Join Date
    Aug 2006
    Posts
    508

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by laserlight View Post
    Why not just provide a constructor for the base class? The constructor initialisation list is for initialising bases and members, not the members of bases.
    The base class is a structure, it is a C toolkit structure and ideally I wouldn't like to change it but derive a C++ class from it just like CRect does from RECT windows structure. I did initialized the const members of base class in c'tor and it didn't complain, so far so good.

    Now I am also doing something very similar to Paul DiLascia's Q/A article http://msdn.microsoft.com/en-us/magazine/cc301420.aspx (first question).

    Now I want
    Code:
    CFooble table[]
    to be a member of another class.
    Code:
    class CMyRect
    {
    public:
      CFooble table[];
    }
    How can I initialize table now give its a member variable? Thanks!

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

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by zspirit
    The base class is a structure, it is a C toolkit structure and ideally I wouldn't like to change it but derive a C++ class from it just like CRect does from RECT windows structure.
    In that case Philip Nicoletti's point applies: the base class is not intended to be a base class, so you should not inherit from it, unless you intend to use private inheritance. Why not just use composition?
    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
    Aug 2006
    Posts
    508

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by laserlight View Post
    In that case Philip Nicoletti's point applies: the base class is not intended to be a base class, so you should not inherit from it, unless you intend to use private inheritance. Why not just use composition?
    Ok I saw Philip Nicoletti's point but I am not aware of this. Why do we need a virtual destructor? I will not be using virtual functions anyways and was just trying to do what MFC has done. Given a plain C/C++ structure and encapsulating it in C++ class. This will mainly help me initialize the C structure in form of a class (array of this structure is actually in a class). My base structure is a plain C structure with no 'ctors at all.I thought RECT in wndow was the same.

    I didn't quite understand why the base class/structure is not intended to be a base class! Could you explain on this please? Thanks.
    Last edited by zspirit; March 9th, 2009 at 12:22 PM.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by zspirit
    Why do we need a virtual destructor?
    You might not need the base class to have a virtual destructor, but if it does not and you write code that requires it, then undefined behaviour comes into play. Read When should my destructor be virtual?

    Quote Originally Posted by zspirit
    I will not be using virtual functions anyways and was just trying to do what MFC has done. Given a plain C/C++ structure and encapsulating it in C++ class.
    The "correct" way to perform such encapsulation is to make an object of that structure type a member of your class.

    Quote Originally Posted by zspirit
    I didn't quite understand why the base class/structure is not intended to be a base class! Could you explain on this please?
    It does not have any virtual functions. Read Stroustrup's answer to the FAQ: What is "OOP" and what's so great about it?
    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

  10. #10
    Join Date
    Aug 2006
    Posts
    508

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by laserlight View Post
    You might not need the base class to have a virtual destructor, but if it does not and you write code that requires it, then undefined behaviour comes into play. Read When should my destructor be virtual?
    I agree and given the purpose I feel it will not be deal breaker here to derive from structure without c'tor as I a merely creating a table of the data, all statically.


    Quote Originally Posted by laserlight View Post
    The "correct" way to perform such encapsulation is to make an object of that structure type a member of your class.
    The question the comes to my mind is what MFC chose to derive from RECT? As far as I know RECT doesn't have virtual destructor.

    Now the next step is my goal to define the able in class instead of:
    Code:
    MYSTRUCT table[] = {
        { 1,2,3 },
        { 4,5,6 },
        ... // etc
    };
    As I pointed out Paul Dilascia explains it http://msdn.microsoft.com/en-us/magazine/cc301420.aspx
    but it doesn't show how the table class can be a member of another class! In that where and how do we initialize! Best Regards.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Initializing base class members in member initializer list

    Eh, but table is an array of MYSTRUCT, not a class.
    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

  12. #12
    Join Date
    Aug 2006
    Posts
    508

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by laserlight View Post
    Eh, but table is an array of MYSTRUCT, not a class.
    Right, the whole point is to package the array table of MYSTRUCT in a class, which can be member variable of another class.

    MYSTRUCT is easy to initialize if it's globle but I don't want it to be gloabal and it has been a pain to initialize it when it's member of another class. I do that by encapsulating it in class where c'tor will take care of initialization. Problem is I can't initialize an array of any structure in another class!

    Some psudo code on the fly, what the problem is:
    Code:
    class A
    {
    A()
    {
    // how to initialize m_s? OK write a class for MYSTRUCT and it's c'tor will
    // initialize each member but  m_s is an array and it's not getting anywere! No initialization possible?
    
    }
    MYSTRUCT m_s[4];
    }

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

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by zspirit View Post
    Right, the whole point is to package the array table of MYSTRUCT in a class, which can be member variable of another class.

    MYSTRUCT is easy to initialize if it's globle but I don't want it to be gloabal and it has been a pain to initialize it when it's member of another class. I do that by encapsulating it in class where c'tor will take care of initialization. Problem is I can't initialize an array of any structure in another class!
    That is because array initialization syntax does not work in the initialization list. You have to assign to the array in the constructor body, or use a container class designed to have initialization built in, i.e. std::vector, std::deque, etc. instead of arrays.

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Aug 2006
    Posts
    508

    Re: Initializing base class members in member initializer list

    In my case array perfectly fits the bill. Each class will have it's own table which is fixed in size. I am fine with initializing the array members in constructor as well but do I have to do it member by member? That would be long lines and less friendly to read with risk of looking like someone is writing his/her first program!

    Lets say may class as member variable (this is code on fly)
    Code:
    struct MYSTRUCT 
    {
        int a;
        int b;
        const float c;
    }
    class A
    {
        MYSTRUCT m_table[5];
    
        A()
        {
            m_table[0].a = 1;
            m_table[0].b = 2;
            m_table[0].c = 1.2;
    
            m_table[1].a = 11;
            m_table[1].b = 19;
            // ... and so on
        }
    }
    So is that the only way to initialize the member array?

    I tried to encapsulate MYSTRUCT into class so it's constructor can initialize it.
    Code:
    class CMYSTRUCT : public  MYSTRUCT;
    {
        CMYSTRUCT(int aa, int bb, float cc)
        {
            a = aa;
            b = bb;
            c = cc;
        }
    }
    
    class A
    {
        CMYSTRUCT m_table[5];
    
        A()
        {
            m_table[0] = {1, 2, 1.2}; // doesn't work
            m_table[1] = {11, 19, 5.5}; // and so but not working
        }
    }
    But this didn't help either. The c'tor is supposed to be responsible for initialization and as long as we have everything in classes, it should be easily facilitated but is c'tor of no use if we have array of the class?

  15. #15
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Initializing base class members in member initializer list

    Quote Originally Posted by zspirit View Post
    In my case array perfectly fits the bill.
    I am strictly speaking in terms of initialization. A vector is an array internally, and doesn't have all of these problems you're having with initialization. So whether array "fits the bill" -- it may in terms of what your program does, but it certainly doesn't fit the bill in terms of how you want to initialize it.

    Basically, you cannot initialize arrays using the initialization syntax. Arrays can only be initialized where they are declared. Since the array is declared as one of the members, you cannot initialize it (since again, the array initialization syntax using { } doesn't work). It is just futile to keep trying things that amount to illegal C++ syntax.

    That's why you either drop using raw arrays and use container classes that are basically arrays anyway, or you have to assign each element in the array their respective values. Until or unless C++ changes its rules, those are the choices you have.
    it should be easily facilitated but is c'tor of no use if we have array of the class
    The constructor is "of use" -- it is the array syntax that is not "of use" to you, since it isn't allowed where you are trying to use it.

    Regards,

    Paul McKenzie

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center