从模板化父类访问继承变量
请考虑以下代码:
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}
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 this scope
g ++ 2.96和MSVC 6,7,7.1,8和9接受它,(至少)旧的Intel和SGI c ++编译器也是如此。
新的Gnu C ++编译器是否遵守标准?如果他们这样做,继承类背后的基本原理是什么,无法看到受保护的继承成员变量?
另外,如果有的话
int A() { return a; }
在Foo,我收到错误
test.cpp:25: error: there are no arguments to A that depend on a template parameter, so a declaration of A must be available test.cpp:25: error: (if you use -fpermissiveâ, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
当我尝试在Bar的成员函数中使用它时。我发现这也很奇怪:Bar继承了Foo,所以我认为Bar的范围内的A()显然是Foo :: A()。
慕斯709654
相关分类