Ash*_*hot 17 c++ inheritance templates g++ static-members
#include <iostream>
#include <string>
class Base
{
static std::string s;
};
template<typename T>
class Derived
: Base
{
public:
Derived()
{
std::cout << s << std::endl;
}
};
std::string Base::s = "some_text";
int main()
{
Derived<int> obj;
}
Run Code Online (Sandbox Code Playgroud)
该程序编译并正常运行.静态变量s
在基类中是私有的,私有地继承.Derived类如何访问它?
如果Derived类不是模板,则编译器会抱怨访问私有变量.
[aminasya@amy-aminasya-lnx c++]$ g++ --version
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
Bar*_*rry 17
这绝对是一个GCC错误,相当于GCC Bug 58740:
class A {
static int p;
};
int A::p = 0;
template<int=0>
struct B : A {
B() {(void)p;}
};
int main() {
B<>();
}
Run Code Online (Sandbox Code Playgroud)
该错误仍处于打开状态,此代码仍在5.1上编译.GCC存在模板成员访问问题,这只是另一个这样的例子.