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.
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 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.