Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;中私有静态成员变量的继承_C++_Templates_Inheritance_Static Members - Fatal编程技术网

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变得更好了。可能是