Originally Posted by
7stud
Your code won't compile for me. If I try to create a ProtectedBase object, I get linker errors(VC6).
Correction: the example compiles with VC6. It does not LINK.
That's because the example only included the declaration of Base. It did not include implementation of Base's member functions.
Originally Posted by
7stud
And I don't see how it can prevent a class derived from ProtectedBase from defining functions named SomeMethod() and SomeOtherMethod().
I didn't claim it would do that. It is impossible to prevent any class from defining member functions of any name (unless that name is a keyword).
What I provided is a *partial* solution to the problem, in that it prevents some behaviours normally associated with virtual functions in classes derived from ProtectedBase.
ProtectedBase provides a member named SomeMethod, which cannot be overridden as a virtual function, despite the fact it calls Base's virtual function named SomeMethod(). While it is not possible to prevent a class derived from ProtectedBase defining a member named SomeMethod(), it does prevent a version of SomeMethod() in that derived class being called as a virtual function.
The SomeOtherMethod() in ProtectedBase remains virtual and can therefore be overridden.
Try this usage;
Code:
class Something : public ProtectedBase
{
public:
void SomeMethod() {};
void SomeOtherMethod() {};
};
int main()
{
ProtectedBase *object = new Something; // assume we have a public default constructor and suitable virtual destructors
object->SomeMethod(); // will invoke ProtectedBase::SomeMethod() which will invoke Base::SomeMethod(), NOT Something::SomeMethod();
object->SomeOtherMethod(); // will invoke Something::SomeOtherMethod()
}