第5章 技巧性基础:5.3 this->的使用

5.3 Using this->

5.3 this->的使用

For class templates with base classes that depend on template parameters, using a name x by itself is not always equivalent to this->x, even though a member x is inherited. For example:

对于具有基类的类模板(其基类依赖于模板参数),使用名称x本身往往与this->是不等同的。即使成员x是继承的。例如:

template<typename T>
class Base {
public:
    void bar();
};

template<typename T>
class Derived : Base<T> {
public:
    void foo() {
        bar(); // 调用外部的bar()或者出错
    }
};

In this example, for resolving the symbol bar inside foo(), bar() defined in Base is never considered. Therefore, either you have an error, or another bar() (such as a global bar()) is called.

在这个例子中,为了解析foo()中bar符号,从不考虑基类定义的bar()。因此,这里不是出错,就是调用另一个bar()(如全局的bar())。

We discuss this issue in Section 13.4.2 on page 237 in detail. For the moment, as a rule of thumb, we recommend that you always qualify any symbol that is declared in a base that is somehow dependent on a template parameter with this-> or Base<T>::.

我们将在第237页的13.4.2中详细讨论这个问题。目前,作为经验法则:对于那些在基类中声明,并且依赖于模板参数的符号,我们始终建议你应该在它们的前面使用this->或base<T>::加以限定。

猜你喜欢

转载自www.cnblogs.com/5iedu/p/12731331.html
今日推荐