child classes calling parent constructors (with arguments)

This is a discussion on child classes calling parent constructors (with arguments) within the C++ Programming forums, part of the General Programming Boards category; hi all, I have a parent and child class. The constructor for the parent class accepts three arguments and would ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    50

    child classes calling parent constructors (with arguments)

    hi all,

    I have a parent and child class. The constructor for the parent class accepts three arguments and would correctly initialise an instance of the child class.
    Is it possible to arrange it so that when an instance of the child class is created, it is constructed with its parent class constructor?

    At the moment, I call the parent class constructor explicitly from the child class constructor and define a argument-less protected constructor in the parent class, which it seems the compiler (VC8) also automatically calls at the end of the child constructor. This works, but it strikes me as a little hack-ish and i'm interested to know if there is a better way of doing this.

    An example:

    Code:
    class ParentClass
    {
    public:
    ParentClass(int arg1, int arg2, int arg3);
    protected:
    ParentClass(){}
    //etc
    };
    
    class ChildClass : public ParentClass
    {
    public:
    ChildClass(int arg1, int arg2, int arg3) {::ParentClass(arg1, arg2, arg3); //compiler inserts a call to ::ParentClass() also}
    //etc
    };
    thanks,

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    The parent constructor is called when you create an instance of its child class. The child class is after all, also the parent class. If you don't have a default constructor, though (which you should), you can call the constructor in your base member initialization list
    Code:
    ChildClass::ChildClass(int arg1, int arg2) : ParentClass(1, 2, 3), cInt1(arg1), cInt2(arg2) {}
    As for your example... there is no point in passing the arguements to the parents constructor if you already have them in the child constructor. You might as well just send their values direct to the data members.
    Last edited by SlyMaelstrom; 05-01-2006 at 06:53 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    50
    Quote Originally Posted by SlyMaelstrom
    The parent constructor is called when you create an instance of its child class. The child class is after all, also the parent class. If you don't have a default constructor, though (which you should), you can call the constructor in your base member initialization list
    Code:
    ChildClass::ChildClass(int arg1, int arg2) : ParentClass(1, 2, 3), cInt1(arg1), cInt2(arg2) {}
    As for your example... there is no point in passing the arguements to the parents constructor if you already have them in the child constructor. You might as well just send their values direct to the data members.
    The base class constructor does quite a lot of processing on the arguments - enough to make the overhead of passing the arguments through irrelvant.

    I'd prefer to not duplicate code in the child constructor, because I feel this weakens the encapsulation. I'd prefer to only have to update one section of code should I need to, because its too easy to forget its duplicated elsewhere.

    In any case, i'll try out the initialization list syntax.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> If you don't have a default constructor, though (which you should)
    There's no reason to say you should have a default constructor. In fact, you should not have a default constructor unless it makes sense for the class, and many classes don't make sense without some sort of initialization.

    >> You might as well just send their values direct to the data members.
    In most cases those data members will be private in the parent class, so that wouldn't work.

    >> i'll try out the initialization list syntax.
    Using the initialization list to call the parent class constructor is the correct way to initialize the parent class and whatever data it keeps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Close Parent Program using child Program?
    By beon in forum Windows Programming
    Replies: 2
    Last Post: 11-30-2007, 09:24 PM
  2. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 10:01 PM
  3. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  4. Classes: constructors, destructors ???
    By mbeisser21 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2002, 09:33 PM
  5. Replies: 2
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21