非静态非引用数据成员声明变量吗? [英] Are non-static non-reference data member declarations variables?

查看:113
本文介绍了非静态非引用数据成员声明变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据答案重新解答一个问题,并附加以下内容的讨论:
为什么非静态数据成员引用不是变量?:

I want to reshoot a question based on the answer and appending discussion of:
Why is a non static data member reference not a variable?:

类的非静态数据成员本身不会创建新变量,而只是帮助您定义类的属性.如果确实创建了一个新变量,则可以编写如下代码:

A non-static data member of class doesn't create a new variable itself, it just helps you to define the properties of the class. If it did create a new variable, you'd be able to write code like this:

class Chamber {
public:
    int pot;
};

void f(bool b) {
    if (b)
    Chamber::pot = 2;
}

那甚至意味着什么?它会找到Chamber的每个实例并将其所有底池设置为2吗?这是胡说八道.

What would that even mean? Would it find every instance of Chamber and set all their pots to 2? It's a nonsense.


但是,在当前的 n4296 c ++ 17草稿变量仍定义为(§3.6):


However, in the current n4296 c++17 draft a variable is still definened as (§3.6):

通过声明非静态数据成员或非静态数据成员的引用来引入变量 目的.变量的名称(如果有的话)表示引用或对象.

A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable’s name, if any, denotes the reference or object.

接受答案中的论点对我来说似乎合乎逻辑,但是基于我对上述变量定义的理解,它与标准相冲突.

The argumentation in the accepted answer seems logical to me but it is, based on my understanding of the variable-definition above, conflicting with the standard.

问题是非静态非引用数据成员声明变量,如果是,为什么会这样,因为我无法以直观的方式使用它们(例如,Chamber::pot答案中的示例)?

Question Are non-static non-reference data member declarations variables, and if yes, why are they considered to be so since I cannot use them in an intuitive fashion ( as e.g. Chamber::pot from the example in the answer ) ?

推荐答案

不,正如Igor Tandetnik在评论中指出的那样,非静态数据成员声明绝不是变量,因为它们决不是对象.它们是使成员具有对象类型的声明,但是在创建类的实例之前没有对象.

No, as Igor Tandetnik pointed out in the comments, non-static data member declarations are never variables, because they are never objects. They are declarations that give the member an object type, but there are no objects until an instance of the class is created.

引用 Richard Smith :

我认为问题在于引用声明"的含义不明确.我相信这里的目的是引用的声明"是一个声明,该声明声明特定名称为引用类型(引用类型的非静态数据成员声明确实如此),而不一定是导致生命周期的声明.开始的特定参考.相反,对象的声明"应解释为声明一个声明特定名称以命名特定对象的声明(非静态数据成员声明不声明).

I suppose the problem is in the ambiguity of what a "declaration of a reference" is. I believe the intent here is that a "declaration of a reference" is a declaration that declares a particular name to be of reference type (which a non-static data member declaration of reference type does), not necessarily a declaration that causes the lifetime of a particular reference to begin. Conversely, a "declaration of an object" is intended to be interpreted as a declaration that declares a particular name to name a specific object (which a non-static data member declaration does not).

也就是说:

  struct A {
    int &a;
    int b;
  };

'a'是引用的声明,因此我们需要另一个条件来限制它成为变量. "b"是/not/对象的声明,因此我们无需赘述.

'a' is a declaration of a reference, and so we need another condition to restrict it from being a variable. 'b' is /not/ a declaration of an object, so we don't need to say anything further.

欢迎提出有关如何重新措词以使其更清楚的建议:)

Suggestions on how to reword this to make it clearer would be welcome :)

此后为 https://github.com/cplusplus/draft/issues/1280 ,发现一些不一致之处.幸运的是,这将被视为一次同时清除该标准的好机会.

This was followed up as https://github.com/cplusplus/draft/issues/1280, some inconsistencies were found. With some luck, this will be seen as a good opportunity to clear up the standard at the same time.

这篇关于非静态非引用数据成员声明变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆