I need to cast a class to its protected base:
class ComplicatedOne : public Object {
//Lots of funcs I don't want or need.
};
class Line : protected ComplicatedOne {
//Some funcs of ComplicatedOne get re-implemented or called by alias here
};
class Array {
void Add (Object &obj);
};
main() {
Array a;
a.Add(new Line());
}
I need to add a Line to an Array but the Object base is not visible. Should I re-impliment its methods publicly or is there an implicit cast operator I could override?
With this, you’re telling the compiler that you can’t implicitly convert a
Lineto anObject:But it seems to me that you want to do this, and also that you should do this. So make the inheritance public. This is a design question.
Don’t make the inheritance
protectedjust to keep methods in the base classprotected.One other option is to implement the cast operator in Line:
and call the function like so:
instead of
You can’t implicitly cast pointers in this situation. However I suggest changing the inheritance type.