I have encountered a piece of code with method being exposed via public interface while the implementation is private. I'm not sure what should be the expected behavior. Simplified example:
#include <iostream>
class Interface
{
public:
virtual ~Interface() {}
virtual void myIfMethod() = 0;
};
class Derived : public Interface
{
private:
void myIfMethod(){std::cout << "private method invoked via public interface" << std::endl;}
};
int main()
{
Interface* myObj = new Derived;
myObj->myIfMethod();
delete myObj;
return 0;
}
This sample compiles and executes without a warning: http://ideone.com/1Ouwk4
Is this a correct and well-defined behavior? And if so, why?
Note, the question isn't about private interface method with public implementation (there are multiple such questions on SO) but the other way around.