mercredi 29 juin 2016

Inheritance wrong call of constrostructors

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?

Aucun commentaire:

Enregistrer un commentaire