Private implementation for the common interface method

I came across a piece of code with a method open via an open interface, while the implementation is private. I'm not sure what should be the expected behavior. A 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 example compiles and runs without warning: http://ideone.com/1Ouwk4

Is this the correct and well-defined behavior? And if so, why?

Please note that the question is not about the private interface method with public implementation (there are several such questions on SO), but vice versa .

+4
source share
1 answer

Standard Draft C ++

[class.access.virt]

1 ( 11) , .

2 , , - (B * ). - , (D ), .

Interface, - , . - . .

, private, .

+1

Source: https://habr.com/ru/post/1624951/


All Articles