I have an abstract class that I have derived from two child classes. One of them has a protected variable that the other one does not. To make the code more general, I have to use an smart pointer of the abstract class. Is there any way to access the protected variable through the pointer? As an example, Consider the following code (the real code is huge and I had to write this sample code):
class Pen{
public:
pen(string _color): color(_color){};
getColor(){return color;};
protected:
string color;
};
// base abstract class
class writer{
public:
writer() {}
virtual changeColor(string color) = 0;
};
class oldWriter: public writer{
protected:
Pen *pen;
public:
oldWriter(string _pen):
pen(_pen){}
virtual changeColor(string color){ pen->color = color;};
};
class youngWriter: public writer{
protected:
Pen *pen;
Pencile pencil; //we need to have access to pencil
public:
youngWriter(string _pen):
pen(_pen){}
virtual changeColor(string color){ pen->color = color;};
Pencil getPencil(){return pencil;};
};
int main(){
unique_ptr<Writer> artist;
Pencil pencil = artist->getPencil(); //how?
}
How can we access "pencil" in "youngWriter" class through "artist"?
Aucun commentaire:
Enregistrer un commentaire