constructor inheritance

Yes I'm a newb at C++ mostly used C or Perl in past.

Anyway I am puzzled by constructor inheritance if I have a base class and derived class I'm trying to avoid having the base class constructor called when I make a derived class object

Example

class Mybase{

public :
Mybase() {cout << "Mybase constructor called"}
};

class MyDerived : public Mybase {

public
MyDerived() {cout << "MyDerived constructor called"}

};


So the issue I see is when I create a MyDerived object the Mybase contructor is called instead.

Example

MyDerived * myderived_ptr = new MyDerived;

This calls Mybase constructor called instead of MyDerived construtor :(

Im using g++ on RHEL 6.3 if that matters.

Also I am attempting to create a list of MyDerived objects within Mybase constructor if somehow that is causing the issue.

Thanks !!

Your program uses both constructors. But your code won't compile.

Please follow up this thread with some program output.
That obviously isn't code you've compiled and run.

For any derived class object, a base class constructor must be called. For a variation of your code that actually compiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

class Mybase{

public :
Mybase() { std::cout << "Mybase constructor called\n"; }
};

class MyDerived : public Mybase {

public:
MyDerived() {std::cout << "MyDerived constructor called\n"; }

};


int main()
{
    MyDerived c ;
}


The output is:

Mybase constructor called
MyDerived constructor called
Yes the code I pasted was just an example. I'm trying to avoid having the Mybase constructor called when I create a MyDerived. The reason for this is I am trying to make a dynamic list of MyDerived objects within the MyBase constructor. So if Mybase constructor gets call when I make a MyDerived then it become a wicked wicked recursive loop :)

I am porting some code and messing around with inheritance as a learning exercise. ... so if is crazy to have base constructor create objects of derived class that is a good learning point for my future c++ way of thinking :)
I'm trying to avoid having the Mybase constructor called when I create a MyDerived.


You cannot do this, the base class portion of your derived object must be constructed.
Thx cire .. that is too bad I guess since an Derived is basically an extension of a base class it makes sense both constructors would be called.
closed account (3qX21hU5)
I'm not sure why you wouldn't want the base class's constructor called? A constructor is meant to initialize the classes objects and a derived class is meant to use some if not all of the base classes interface. So for a derived class to be able to use the base's interface the base class has to be initialized or else it wouldn't be able to use certain parts of the bases interface.

At least that is how I look at it, which could very well be wrong.

If you don't want it to call your other class's constructor don't have it derive from it.

Last edited on
Hello yes I agree. I am learning the C++ .. haven't done much with it for 10 +yrs .. anyway I agree not to inherit from base
Topic archived. No new replies allowed.