c++ - Initialize the protected static member from the subclass -


i want know if it's possible initialize protected static member subclass.
example,

// head file class test { protected:     static int i; }; class test2 : public test{};  //cpp file #include "headfile.h" int test2::i = 1; 

as see, when initialize static member (i), use subclass name (test2).
surprise, tested code visual studio 2013 , worked without error. if tried netbeans(gcc11) under linux , got hint error:
unable resolve identifier i
compiled it, error message is:
error: iso c++ not permit ‘test::i’ defined ‘test2::i’ [-fpermissive]

now if change protected public static int i in class test, error disappear.

i confused... first time found 2 different results gcc , vs.

the definition violates c++14 [class.static.data] §9.4.2/2. emphasis mine:

in definition @ namespace scope, name of static data member shall qualified its class name using :: operator.

a more recent version of gcc (on coliru) behaves same regardless of qualifier. can defeat error on gcc -fpermissive, note you're still defining 1 object, belonging base class.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -