有任何理由使用this-> [英] Is there any reason to use this->

查看:88
本文介绍了有任何理由使用this->的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中编程了很多年,我仍然怀疑一件事。在许多地方的其他人的代码我看到像:

I programmed in C++ for many years and I still have doubt about one thing. In many places in other people code I see something like:

void Classx::memberfunction()
{
    this->doSomething();
}



如果我需要导入/使用该代码, > this - > 部分,我从未见过任何破碎或有副作用。

If I need to import/use that code, I simply remove the this-> part, and I have never seen anything broken or having some side-effects.

void Classx::memberfunction()
{
    doSomething();
}

那么,你知道使用这种结构的任何原因吗?

So, do you know of any reason to use such construct?

编辑:请注意,我在这里谈论成员函数,而不是变量。我理解当你想区分成员变量和函数参数时,可以使用它。

Please note that I'm talking about member functions here, not variables. I understand it can be used when you want to make a distinction between a member variable and function parameter.

编辑:显然重复:
http://stackoverflow.com/questions/333291/are-there-any

apparent duplicate: http://stackoverflow.com/questions/333291/are-there-any-reasons-not-to-use-this-self-me

推荐答案

为了保证触发编译器错误,有一个宏可能被定义与您的成员函数同名,你不确定它是否可靠未定义。

To guarantee you trigger compiler errors if there is a macro that might be defined with the same name as your member function and you're not certain if it has been reliably undefined.

没有开玩笑,我非常肯定,我不得不这样做的原因!

No kidding, I'm pretty sure I've had to do exactly this for that reason!

这篇关于有任何理由使用this->的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆