它适用于常规课程:
class Base
{
public:
Base() {}
protected:
int* a;
};
class Derived : public Base
{
public:
Derived() {}
void foo() {
int** pa = &a;
}
};
int main() {
Derived* d = new Derived();
d->foo();
delete d;
}
Base
但是当和Derived
类使用模板时会报错:
'int* Base<int>::a' 在此上下文中受到保护
template<typename T>
class Base
{
public:
Base() {}
protected:
int* a;
};
template<typename T>
class Derived : public Base<T>
{
public:
Derived() {}
void foo() {
int** pa = &Base<T>::a;
}
};
int main() {
Derived<int>* d = new Derived<int>();
d->foo();
delete d;
}
这是为什么?