这段代码是模棱两可的还是完全没问题(通过标准批准/对现有的任何编译器都有一致的行为)?
struct SCustomData {
int nCode;
int nSum;
int nIndex;
SCustomData(int nCode, int nSum, int nIndex)
: nCode(nCode)
, nSum(nSum)
, nIndex(nIndex)
{}
};
编辑:
是的,我指的是成员变量与构造函数的形参同名。
最佳答案
不,在这种情况下没有歧义,但请考虑以下内容:
struct SCustomData {
//...
void SetCode(int nCode)
{
//OOPS!!! Here we do nothing!
//nCode = nCode;
//This works but still error prone
this->nCode = nCode;
}
};
您应该注意一种现有的编码风格。例如General Naming Rule in Google C++ Coding Styles或阅读 Herb Sutter 和 Andrei Alexandrescu 的优秀著作“C++ Coding Standards: 101 Rules, Guidelines, and Best Practices”。
关于c++ - 这是模棱两可还是完全没问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2161397/