我读过的与我的问题相关并回答了部分问题的帖子:
我的问题:
我的具体问题来自这里的主题:
该主题中提供的关于 C++ 歧义的答案是否相同?this
如果我的参数与我的字段具有相同的名称,我是否只使用它很重要?
这是一个带有this
关键字的示例:
Gui::Gui()
{
this->organismSize = 10.0;
this->foodSize = 5.0;
}
没有this
关键字:
Gui::Gui()
{
organismSize = 10.0;
foodSize 5.0;
}
除非类属性和参数具有相同的名称,否则无需在构造函数中使用“this”关键字。
Gui::Gui(const double organismSize, const double foodSize )
{
organismSize =organismSize; // we have an ambiguity, the compiler cannot make a difference between the parameter and the attribute
foodSize = foodSize ;
}
Gui::Gui(const double organismSize, const double foodSize )
{
this->organismSize =organismSize; // the compiler can make a difference between the parameter and the attribute
//this->organismSize is the attribute while organismSize is the parameter
this->foodSize = foodSize ;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句