更新时间:2023-02-15 17:46:10
这可以通过使用 using
将名称拉入当前范围来修改:
This can be amended by pulling the names into the current scope using using
:
template<typename T> struct Subclass : public Superclass<T> {
using Superclass<T>::b;
using Superclass<T>::g;
void f() {
g();
b = 3;
}
};
或者通过this
指针访问来限定名称:
Or by qualifying the name via the this
pointer access:
template<typename T> struct Subclass : public Superclass<T> {
void f() {
this->g();
this->b = 3;
}
};
或者,正如您已经注意到的,通过限定全名.
Or, as you’ve already noticed, by qualifying the full name.
之所以有必要这样做,是因为 C++ 不考虑用于名称解析的超类模板(因为它们是从属名称并且不考虑从属名称).它在您使用 Superclass
时有效,因为它不是模板(它是模板的实例化),因此它的嵌套名称不依赖 名字.
The reason why this is necessary is that C++ doesn’t consider superclass templates for name resolution (because then they are dependent names and dependent names are not considered). It works when you use Superclass<int>
because that’s not a template (it’s an instantiation of a template) and thus its nested names are not dependent names.