什么时候应该使用?

jma*_*erx 5 c++

我想知道是否应该使用这两个:

void SomeClass::someFunc(int powder)
{
     this->powder = powder;
}

//and
void SomeClass::someFunc(bool enabled)
{
     this->isEnabled = enabled;
}
Run Code Online (Sandbox Code Playgroud)

我想知道后者是否必须正确或如果isEnabled =启用就足够了.

谢谢

Mai*_*ter 4

this->\n
Run Code Online (Sandbox Code Playgroud)\n\n

直接使用成员时需要会产生歧义。模板代码可能会发生这种情况。

\n\n

考虑一下:

\n\n
#include <iostream>\n\ntemplate <class T>\nclass Foo\n{\n   public:\n      Foo() {}\n   protected:\n      void testing() { std::cout << ":D" << std::endl; }\n};\n\ntemplate <class T>\nclass Bar : public Foo<T>\n{\n   public:\n      void subtest() { testing(); }\n};\n\nint main()\n{\n   Bar<int> bar;\n   bar.subtest();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这将失败,因为调用testing()依赖于模板参数。说你的意思是你必须执行的功能this->testing();Foo<T>::testing();

\n\n

错误信息:

\n\n
temp.cpp: In member function \xe2\x80\x98void Bar<T>::subtest()\xe2\x80\x99:\ntemp.cpp:16:32: error: there are no arguments to \xe2\x80\x98testing\xe2\x80\x99 that depend on a template parameter, so a declaration of \xe2\x80\x98testing\xe2\x80\x99 must be available [-fpermissive]\ntemp.cpp:16:32: note: (if you use \xe2\x80\x98-fpermissive\xe2\x80\x99, G++ will accept your code, but allowing the use of an undeclared name is deprecated)\n
Run Code Online (Sandbox Code Playgroud)\n