Skip to content

Instantly share code, notes, and snippets.

@odeblic
Last active July 27, 2017 11:31
Show Gist options
  • Save odeblic/706820c998e3410155730149c8b29944 to your computer and use it in GitHub Desktop.
Save odeblic/706820c998e3410155730149c8b29944 to your computer and use it in GitHub Desktop.
Legal member access violation with polymorphism
#include <iostream>
class Base
{
public:
virtual void action() { std::cout << "Base::action()\n"; }
};
class Derived : public Base
{
private:
void action() override { std::cout << "Derived::action()\n"; }
};
int main(void)
{
Derived derived;
Base& base = derived;
// derived.action(); // does not build!
base.action(); // call the private method!
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment