如何从模板化基类调用模板化派生类上的成员
How can I call a member from a templated base class on a templated derived class?
这样设置:
template<int N>
struct Base {
void foo();
};
class Derived : Base<1> {
static void bar(Derived *d) {
//No syntax errors here
d->Base<1>::foo();
}
};
一切正常。然而,对于这个例子:
template<class E>
struct Base {
void foo();
};
template<class E>
class Derived : Base<E> {
static void bar(Derived<E> *d) {
//syntax errors here
d->Base<E>::foo();
}
};
我:
error: expected primary-expression before '>' token
error: '::foo' has not been declared
有什么区别?为什么第二个会导致语法错误?
前提是你的代码在Clang 3.2(见这里)和GCC 4.7.2(见这里)上可以很好地编译,我不认为有理由使用Base<E>::
:只使用d->foo()
:
template<class E>
struct Base {
void foo() { }
};
template<class E>
struct Derived : Base<E> {
static void bar(Derived<E> *d) {
//syntax errors here
d->foo();
}
};
int main()
{
Derived<int> d;
Derived<int>::bar(&d);
}
或者,您可以尝试使用template
消歧器:
d->template Base<E>::foo();
相关文章:
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 如何使用基类指针引用派生类成员
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 在 C++ 中用派生类型重写成员函数
- 如果基类包含双指针成员,则派生类的构造函数
- 在派生类中绑定非静态模板化成员函数
- 为什么派生类的好友不能使用受保护的成员?
- 在派生类中使用基类的私有成员变量的最佳方法
- 派生类如何获取基的成员?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 调用派生类成员函数
- 从模板化父类中的派生内部类访问受保护的成员变量
- 将基类的成员函数重载到其他派生类C++
- 派生的成员 通过指向基、static_cast、crtp、删除模板的指针
- C++ 使用派生类方法更改基类数据成员
- 派生类看不到基类成员
- 使用派生类中的静态成员而不是基类
- 是否可以使用 using 将基类中的公共成员重新声明为派生类中的私有成员?
- 如何将派生类中的成员函数作为回调传递?