<分区>
我想访问父类中的 protected 变量,我有以下代码并且编译正常:
class Base
{
protected:
int a;
};
class Child : protected Base
{
public:
int b;
void foo(){
b = a;
}
};
int main() {
Child c;
c.foo();
}
好的,现在我想把所有的东西都模板化。我将代码更改为以下内容
template<typename T>
class Base
{
protected:
int a;
};
template <typename T>
class Child : protected Base<T>
{
public:
int b;
void foo(){
b = a;
}
};
int main() {
Child<int> c;
c.foo();
}
出现错误:
test.cpp: In member function ‘void Child<T>::foo()’:
test.cpp:14:17: error: ‘a’ was not declared in this scope
b = a;
^
这是正确的行为吗?有什么区别?
我使用 g++ 4.9.1