Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c++ - Are non-static non-reference data member declarations variables?

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;
}

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


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.

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 ) ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Quoting 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).

That is:

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

'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 :)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...