I have this abstract class in C++:
class A {
public:
A(int size);
virtual void doSomething(int inputSize) = 0;
protected:
virtual bool checkSize(int inputSize);
private:
int size;
}
A::A(int size) : size(size){}
bool A::checkSize(int inputSize) { return size == inputSize; }
Now, what I want to guarantee is that for each class B
derived from A
doSomething
begins like this:
class B : public A{
void doSomething(int inputSize);
}
void B::doSomething(int inputSize){
if(!checkSize(inputSize)){
std::cerr<<"wrong inputSize!"<<std::endl;
return;
}
//do something...
}
How can I guarantee that each derived class from A
implements doSomething
starting in that way?
You split
doSomething
into two parts:And in
B
you only implementdoSomething_
.