I have an abstract base class for Data type and two inherited class, Data1 and Data2. Data1 and Data2 have different members. I have an abstract class Proc which operates on these data. Proc has a virtual function update which takes a reference of Data as parameter. Proc1 and Proc2 both define its own implementation. However both of the implementation will use members defined in concrete class Data1 and Data2. How to make this work given the parameter is a reference of Data? For example.
class Data {};
class Data1: public Data{
int x;
};
class Data2: public Data{
string str;
};
class Proc{
virtual void update(const Data&) = 0;
};
class Proc1: public Proc{
update(const Data &d){
// this does not work
cout << x << endl;
}
};
class Proc2: public Proc{
update(const Data &d){
// this does not work
cout << str << endl;
}
};
Thank you for suggestions.
Aucun commentaire:
Enregistrer un commentaire