Thread: Is it legal to initilize base class members in a derived class initilizer list?

  1. #1
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Is it legal to initialize base class members in a derived class initializer list?

    The title says it all. I have class surface_base with a single member: m_data, a pointer to raw surface data. class surface (derived from surface_base) adds some extra constructors which try to initilize m_data in the initilizer list...

    The error I get is "surface.hpp(39) : error C2614: 'surface' : illegal member initialization: 'm_data' is not a base or member" in MSVC++ 6.

    Either it's illegal to do this, or I have a syntax error somewhere else and the compiler is giving me misleading error messages.
    Last edited by Eibro; 12-12-2002 at 08:53 AM.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    it's illegal -- you have to use one of the base class's constructors. You call the appropriate constructor in the initializer list just like you'd initialize a variable in a class IE

    Code:
    class A
    {
    public:
        A() {}
        A( int Init ) : Var( Init ) {}
    protected:
        int Var;
    };
    
    class B
        : public A
    {
    public:
        B() {}
        B( int A_Init, int B_Init ) : A( A_Init ), Var2( B_Init ) {}
    protected:
        int Var2;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Inheritance: assign base class to derived class
    By MWAAAHAAA in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2007, 04:31 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM