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

发布于 2024-08-19 05:51:56 字数 328 浏览 3 评论 0原文

这段代码是否含糊不清或者是否完全正确(通过标准批准/对现有的任何编译器具有一致的行为)?

struct SCustomData {
    int nCode;
    int nSum;
    int nIndex;
    SCustomData(int nCode, int nSum, int nIndex)
        : nCode(nCode)
        , nSum(nSum)
        , nIndex(nIndex)
    {}
};

编辑:
是的,我指的是成员变量与构造函数的形参同名。

Is this code ambiguous or is it perfectly fine (approved by standards/has consistent behavior for any compilers in existence)?

struct SCustomData {
    int nCode;
    int nSum;
    int nIndex;
    SCustomData(int nCode, int nSum, int nIndex)
        : nCode(nCode)
        , nSum(nSum)
        , nIndex(nIndex)
    {}
};

edit:
yes, I am referring to the fact that the member variables have the same name with formal parameters of the constructor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月牙弯弯 2024-08-26 05:51:56

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

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

            //This works but still error prone
            this->nCode = nCode;
    }
};

您应该注意现有的编码风格之一。例如,Google C++ 编码样式中的一般命名规则或阅读优秀文章Herb 所著的《C++ 编码标准:101 条规则、指南和最佳实践》一书萨特和安德烈·亚历山德雷斯库。

No, in this case there are no ambiguity, but consider following:

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

            //This works but still error prone
            this->nCode = nCode;
    }
};

You should draw attention to one of existing coding styles. For instance General Naming Rule in Google C++ Coding Styles or read excellent book "C++ Coding Standards: 101 Rules, Guidelines, and Best Practices" by Herb Sutter and Andrei Alexandrescu.

浮世清欢 2024-08-26 05:51:56

你的例子(对我来说)是明确的,但这不是一个好的做法,因为它很快就会变得非常模糊。

我已经很长一段时间没有在愤怒中写过任何 C++ 了,所以我猜下面会做什么。
知道它会做什么吗?你确定吗?

class Noddy
{
    int* i;
    Noddy(int *i)
    : i(i)
    {
        if(i == NULL)
            i = new int;
    }
};

Your example is unambiguous (to me), but it's not good practise, because it can quickly become as ambiguous as hell.

It's a long while since I've written any C++ in anger, so I'm guessing what the following will do.
Do you KNOW what it will do? Are you sure?

class Noddy
{
    int* i;
    Noddy(int *i)
    : i(i)
    {
        if(i == NULL)
            i = new int;
    }
};
话少情深 2024-08-26 05:51:56

如果您指的是对成员和构造函数参数使用相同的名称,那么这绝对没问题。然而,您可能会发现有些人出于某种原因坚持认为这是不好的做法。

如果您需要访问构造函数主体中的成员,那么您需要小心 - 要么为参数提供不同的名称,要么使用 this-> 来访问成员。

如果您指的是使用伪匈牙利疣来提醒人们整数就是整数,那么这在技术上是允许的,但绝对没有任何好处,并且会使代码更难以阅读。请不要这样做。

If you're referring to using the same name for members and constructor arguments, then that's absolutely fine. However, you might find some people who insist that it's bad practice for some reason.

If you need to access the members in the constructor body, then you need to be careful - either give the arguments different names, or use this-> to access members.

If you're referring to using pseudoHungarian warts to remind people that integers are integers, then that is technically allowed, but has absolutely no benefits and makes the code much harder to read. Please don't do it.

清醇 2024-08-26 05:51:56

一般来说,我在构造函数中使用下划线为实例变量添加前缀,并在没有任何前缀的情况下为命名参数添加前缀。至少,这将消除参数与实例变量的歧义。如果在构造函数体内进行初始化,它也会让生活变得不那么忙碌。

struct SCustomData {
    int _nCode;
    int _nSum;
    int _nIndex;
    SCustomData(int nCode, int nSum, int nIndex)
        : _nCode(nCode), _nSum(nSum), _nIndex(nIndex)
    {}
};

// Alternatively
struct SCustomData {
    int _nCode;
    SCustomData(int nCode)
    {
        this->_nCode = nCode;
    }
};

我不喜欢按照问题中编写的方式堆叠变量。我喜欢节省垂直空间,而且从左到右阅读也更容易。 (这是我个人的喜好,不是强制性规则或类似的东西。)

In general, I've prefixed instance variables with underscores and named parameters in the constructor without any prefixes. At the very least, this will disambiguate your parameters from your instance variables. It also makes life less hectic if initializing within the body of the constructor.

struct SCustomData {
    int _nCode;
    int _nSum;
    int _nIndex;
    SCustomData(int nCode, int nSum, int nIndex)
        : _nCode(nCode), _nSum(nSum), _nIndex(nIndex)
    {}
};

// Alternatively
struct SCustomData {
    int _nCode;
    SCustomData(int nCode)
    {
        this->_nCode = nCode;
    }
};

I don't like stacking the variables the way it was written in the question. I like to save vertical space, and it's also easier for me to read left-to-right. (This is a personal preference of mine, not a mandatory rule or anything like that.)

天生の放荡 2024-08-26 05:51:56

我想说这完全没问题。

对于使用初始化列表并且没有任何代码的构造函数,这是我的首选样式。我认为这可以减少混乱,因为哪个构造函数参数分配给哪个成员是显而易见的。

I would say that this is perfectly fine.

It is my preferred style for constructors that use the initialization list and don't have any code. I think that it reduces confusion because it is obvious which constructor parameter goes to which member.

冷︶言冷语的世界 2024-08-26 05:51:56

它完全符合标准,但是有些编译器不接受与构造函数参数同名的成员变量。事实上,由于这个原因,我不得不更改我的开源库。请参阅此补丁

It is perfectly standard compliant, but there are compilers out there that would not accept member variables having the same name as constructor parameters. In fact, I had to change my open source library for that reason. See this patch

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文