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 m accessible @ point r when named in class n if

  • m member of n public, or
  • m member of n private, , r occurs in member or friend of class n, or
  • m member of n protected, , r occurs in member or friend of class n, or in member of class p derived n, m member of p public, private, or protected, or
  • there exists base class b of n accessible @ r, , m accessible @ r when named in class b.

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

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -