这是模棱两可还是完全没问题?

Afr*_*ief 5 c++ standards constructor coding-style initialization

这个代码是不明确的还是完全没问题(由标准认可/对于现有的编译器具有一致的行为)?

struct SCustomData {
    int nCode;
    int nSum;
    int nIndex;
    SCustomData(int nCode, int nSum, int nIndex)
        : nCode(nCode)
        , nSum(nSum)
        , nIndex(nIndex)
    {}
};
Run Code Online (Sandbox Code Playgroud)

编辑:
是的,我指的是成员变量与构造函数的形式参数具有相同的名称.

Ser*_*kov 6

不,在这种情况下没有歧义,但请考虑以下事项:

struct SCustomData {
//...
    void SetCode(int nCode)
    {
            //OOPS!!! Here we do nothing!
            //nCode = nCode;

            //This works but still error prone
            this->nCode = nCode;
    }
};
Run Code Online (Sandbox Code Playgroud)

您应该引起对现有编码样式之一的注意.例如,Google C++编码样式中的常规命名规则,或阅读Herb Sutter和Andrei Alexandrescu 撰写的优秀书籍" C++编码标准:101规则,指南和最佳实践 ".