Created
February 17, 2013 02:46
-
-
Save kinoshita-lab/4969830 to your computer and use it in GitHub Desktop.
dunno why DHoge::method() is accessible from outside...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.