C ++继承模板类:隐藏成员变量

时间:2018-06-06 05:15:23

标签: c++

为什么下面的代码没有编译?

#include <iostream>
template <class T>
class A
{
  public:
  T data;
};

template<typename T>
class B : public A<T>
{
 public:
 void foo(){printf("%d\n", data);}
};

int main() 
{
  B<int> b;
}

错误:

bla.cpp: In member function ‘void B<T>::foo()’:
bla.cpp:14:30: error: ‘data’ was not declared in this scope
void foo(){printf("%d\n", data);}

似乎成员变量&#34;数据&#34;因某种原因隐藏了。

1 个答案:

答案 0 :(得分:0)

您可以通过this

访问基本成员变量
this->data;
相关问题