c++ - g++ compilation error "... is protected from within this context" while there's no error with clang -
i have following code:
#include <iostream> class baseclass { protected: static int x; }; int baseclass::x; class deriveda: public baseclass { public: deriveda() { x = 3; } }; class derivedb: public baseclass { public: derivedb() { std::cout << deriveda::x; } }; int main(int argc, char* argv[]) { derivedb b; } compiling g++ (g++ classtest.cpp) receive following error:
classtest.cpp: in constructor ‘derivedb::derivedb()’:
classtest.cpp:9:5: error: ‘int baseclass::x’ protected
int baseclass::x;
^ classtest.cpp:25:32: error: within context
std::cout << deriveda::x;
when i'm compiling clang++ (clang++ classtest.cpp) there's no error.
why g++ returning compilation error?
i use g++ version 5.1.0 , clang++ version 3.6.1
gcc bug. [class.access.base]/p5:
a member
maccessible @ pointrwhen named in classnif
mmember ofnpublic, ormmember ofnprivate, ,roccurs in member or friend of classn, ormmember ofnprotected, ,roccurs in member or friend of classn, or in member of classpderivedn,mmember ofppublic, private, or protected, or- there exists base class
bofnaccessible @r, ,maccessible @rwhen named in classb.
n deriveda, m x, r constructor of derivedb. there exists base class baseclass of deriveda accessible @ r, , x named in class baseclass (i.e., baseclass::x) plainly accessible @ r, fourth bullet point, deriveda::x accessible @ r.
Comments
Post a Comment