C++ C++;中私有静态成员变量的继承
我遇到了模板和访问派生类中基类的静态私有成员的问题C++ C++;中私有静态成员变量的继承,c++,templates,inheritance,static-members,C++,Templates,Inheritance,Static Members,我遇到了模板和访问派生类中基类的静态私有成员的问题 #include <iostream> class Foo { public: Foo() {}; ~Foo() {}; private: static int some_variable; }; int Foo::some_variable; template<typename T> class Bar : Foo { public: Bar(T bla) { some_vari
#include <iostream>
class Foo {
public:
Foo() {};
~Foo() {};
private:
static int some_variable;
};
int Foo::some_variable;
template<typename T>
class Bar : Foo {
public:
Bar(T bla) {
some_variable = bla;
};
~Bar() {};
void test() {
std::cout << some_variable << std::endl;
}
};
int main()
{
Foo A;
Bar<int> B(2);
B.test();
}
似乎我可以从模板化的派生类非法访问基类私有成员变量
#include <iostream>
class Foo {
public:
Foo() {};
~Foo() {};
private:
static int some_variable;
};
int Foo::some_variable;
template<typename T>
class Bar : Foo {
public:
Bar(T bla) {
some_variable = bla;
};
~Bar() {};
void test() {
std::cout << some_variable << std::endl;
}
};
int main()
{
Foo A;
Bar<int> B(2);
B.test();
}
#包括
福班{
公众:
Foo(){};
~Foo(){};
私人:
静态int某些_变量;
};
intfoo::一些_变量;
模板
酒吧类别:富{
公众:
巴(T布拉){
某些_变量=bla;
};
~Bar(){};
无效测试(){
std::cout在旧版本的GCC中,模板类的访问说明符被破坏。它可能工作,也可能不工作。(他们甚至对它做了一个累积错误报告…)。GCC 9变得更好了。可能是