Thread: Constructing Base Class Objects

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    Constructing Base Class Objects

    Hello all,

    Does anyone know why can I not put the call to the constructor of a base within the body of the derived class constructor? Here an example:
    Code:
    class A
    {
         int a;
       public:
         A(int ext_a);
    };
    A::A(int ext_a):a(ext_a)
    {}
    
    class test : public A
    {
         int t;
       public:
         test(int a, int ext_t);
    };
    
    test::test(int a, int ext_t)
    {
         A(a);
         t=ext_t;
    }
    I'm particularly interested int the case when a is not given in the call to test::test, but calculated within its body.

    Thanks

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You initialize base classes through initializer lists:
    Code:
    test::test(int a, int ext_t) : A(a), t(ext_t) {}
    You can't calculate a value within the body and then pass it to the base constructor. By the time execution reaches the body, the base must be fully constructed already.
    You can compute the value in another function and put the entire expression in the initializer list.
    Code:
    test::test(int a, int ext_t) : A(compute(a)), t(ext_t) {}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    8
    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You can compute the value in another function and put the entire expression in the initializer list.

    If you do and you use a member function then you must be careful not to use member variables or base class data inside the function. You can use base classes and member variables that have already been initialized in the initializer list, but if you're using the function to initialize a base class (as in this example) the chances are slim that there will be anything usable at that time. If you aren't using member variables or base classes, then the function should probably be a non-member or static member function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 12:43 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. dynamic array of base class pointers
    By Corrington_j in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 05:58 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM