第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>::加以限定。

 

原文链接: https://www.cnblogs.com/5iedu/p/12731331.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/392609

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月3日 下午2:53
下一篇 2023年4月3日 下午2:53

相关推荐