Skip to content

Instantly share code, notes, and snippets.

@kinoshita-lab
Created February 17, 2013 02:46
Show Gist options
  • Save kinoshita-lab/4969830 to your computer and use it in GitHub Desktop.
Save kinoshita-lab/4969830 to your computer and use it in GitHub Desktop.
dunno why DHoge::method() is accessible from outside...
#include <cstdio>
class Hoge
{
public:
Hoge() {}
virtual ~Hoge() {}
virtual void method() {
puts("hoge");
}
};
class DHoge : public Hoge
{
public:
DHoge() {}
virtual ~DHoge() {}
private:
void method() {
puts("dhoge");
}
};
int main(int argc, char const *argv[])
{
Hoge* hoge = new DHoge();
hoge->method();
delete hoge;
return 0;
}
@kinoshita-lab
Copy link
Author

This is a C++ spec
https://github.com/cplusplus/draft

11.5 Access to virtual functions [class.access.virt]
1 The access rules (Clause 11) for a virtual function are determined by its declaration and are not affected by the rules for a function that later overrides it.
2 Access is checked at the call point using the type of the expression used to denote the object for which the
member function is called (B* in the example above). The access of the member function in the class in which it was defined (D in the example above) is in general not known.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment