This question already has an answer here:
- Multiple inheritance with one base class 2 answers
I'm implemeting this diamond inheritance:
class Object {
private:
int id; string name;
public:
Object(){};
Object(int i, string n){name = n; id = i;};
};
class Button: virtual public Object {
private:
string type1;
int x_coord, y_coord;
public:
Button():Object(){};
Button(int i, string n, string ty, int x, int y):Object(i, n){
type = ty;
x_coord = x;
y_coord = y;};
};
class Action: virtual public Object {
private:
string type2;
public:
Action():Object(){};
Action(int i, string n, string t):Object(i, n){ type2 = t;};
};
class ActionButton: public Button, public Action{
private:
bool active;
public:
ActionButton():Buton(), Action(){};
ActionButton(int i, string n, string t1, int x, int y, string t2, bool a):
Button(i, n, t1, x, y), Action(i, n, t2) {active = a;};
};
Everything works fine about the first three classes, but when I try to create an object of the type ActionButton, instead of calling the constructor with the parameters I wrote, it is calling the default one from the class Object. So every ButtonAction object has name an empty string and id a random value. What's wrong with my code and how can i make it work properly?
Virtual bases are constructed directly by the constructor of the most derived class.
Your
ActionButton
constructor doesn't explicitly callObject
's constructor, so the default constructor is called for you.