Suppose I have a super class that offers a public template method. Subclasses will have to implement some sub-operations. How do I declare this sub-ops to make sure they can only be called from SuperClass
? There's protected
, but that works the other way round as far as I know: Subclasses can access protected superclass members. I want to allow a superclasses (and only superclasses!) to call subclass members.
class SuperClass{
public:
void templateMethod(){
this->op1();
this->op2();
}
// how to declare these? public? protected?
virtual void op1() = 0;
virtual void op2() = 0;
}
class SubClass : public SuperClass{
// how to declare these? public? protected?
virtual void op1() { ... };
virtual void op2() { ... };
}
I'm currently working in C++ and Matlab, but I'd also be very interested in some general remarks considering other languages.