Using interface classes and non-virtual interface idiom in C++
- by andreas buykx
Hi all,
In C++ an interface can be implemented by a class with all its methods pure virtual:
class IFoo
{
public:
virtual void method() = 0;
};
Now I want to implement this interface by a hierarchy of classes:
class FooBase : public IFoo // implement interface IFoo
{
public:
void method(); // calls methodImpl;
private:
virtual void methodImpl();
};
For the class hierarchy I would like to use the non-virtual interface (NVI) idiom, to deny derived classes the possibility of overriding the common behavior implemented in FooBase::method(), but it seems that all derived classes have the opportunity to override the FooBase::method() because it is declared in the interface class.
Is my observation correct? And if so are there other options to both use interface classes and the NVI idiom?