相关疑难解决方法(0)

从模板化父类访问继承变量

请考虑以下代码:

template<class T> class Foo
{
public:
  Foo() { a = 1; }

protected:
  int a;
};

template<class T> class Bar : public Foo<T>
{
public:
  Bar() { b = 4; };

  int Perna(int u);

protected:
  int b;

};

template<class T> int Bar<T>::Perna(int u) 
{ 
  int c = Foo<T>::a * 4; // This works
  return (a + b) * u;    // This doesn't
}
Run Code Online (Sandbox Code Playgroud)

g ++ 3.4.6,4.3.2和4.1.2给出错误

test.cpp: In member function `int Bar<T>::Perna(int)':
test.cpp:25: error: `a' was not declared in …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates

28
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

inheritance ×1

templates ×1