C
cppaddict
Hi,
I know that it is illegal in C++ to have a static pure virtual method,
but it seems something like this would be useful when the following 2
conditions hold:
1. You know that every one of your Derived classes will need to
implement some method, but implement it differently, and that the base
class cannot implement it. This is where pure virtual comes in.
2. You know that for every one of your Derived classes, the method
never uses or alters member data. Thus it should be static.
To give a concrete example.
Say you have an abstract Document class, which has a method called
processDocument:
void processDocument() {
if (isApproved(_documentAuthor))
callSomeNonVirtualFunction();
else
callAnotherNonVirtualFunction();
}
Just to take an easy example, let's say that we know isApproved() is
some silly function of the author's name, which is passed in as an
arguement. Maybe for a derived KinkosDocument, isApproved() returns
true if the author's name begins with "A", and is otherwise false.
For some other derived class the rule would be different, but always a
function of the author's name itself.
So we have the following:
1. isApproved() should not be implemented in the base Document class.
2. isApproved() must be implemented in any derived concrete class.
3. isApproved() must be a simple function of a string.
What is the right design for a situation like this?
Thanks,
cpp
I know that it is illegal in C++ to have a static pure virtual method,
but it seems something like this would be useful when the following 2
conditions hold:
1. You know that every one of your Derived classes will need to
implement some method, but implement it differently, and that the base
class cannot implement it. This is where pure virtual comes in.
2. You know that for every one of your Derived classes, the method
never uses or alters member data. Thus it should be static.
To give a concrete example.
Say you have an abstract Document class, which has a method called
processDocument:
void processDocument() {
if (isApproved(_documentAuthor))
callSomeNonVirtualFunction();
else
callAnotherNonVirtualFunction();
}
Just to take an easy example, let's say that we know isApproved() is
some silly function of the author's name, which is passed in as an
arguement. Maybe for a derived KinkosDocument, isApproved() returns
true if the author's name begins with "A", and is otherwise false.
For some other derived class the rule would be different, but always a
function of the author's name itself.
So we have the following:
1. isApproved() should not be implemented in the base Document class.
2. isApproved() must be implemented in any derived concrete class.
3. isApproved() must be a simple function of a string.
What is the right design for a situation like this?
Thanks,
cpp