I have the 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 with g++ (g++ classtest.cpp
) I receive the following error:
classtest.cpp: In constructor ‘DerivedB::DerivedB()’:
classtest.cpp:9:5: error: ‘int BaseClass::x’ is protected
int BaseClass::x;
^ classtest.cpp:25:32: error: within this context
std::cout << DerivedA::x;
When I'm compiling with clang++ (clang++ classtest.cpp
) there's no error.
Why is g++ returning the compilation error?
I use g++ version 5.1.0 and clang++ version 3.6.1