Crackie
Crackie

Reputation: 355

Can an instance of a struct or a class be considered as a variable?

I've read:

(meaning a variable name is just a case of identifiers)

In standard C++ terminology, Can we consider the name of an instance of a struct or a class as a variable? or Should we only call it "an identifier"?

I know it's definitely a noob question but I really couldn't answer the question myself even if I've tried to read the definition of a variable from several sources. Please help.

Upvotes: 2

Views: 225

Answers (2)

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16049

Tl;dr: A variable is usually a named object, or a reference to an object. There are variables which do not have a name and there are those which do not have an associated object, but those are exceptions.


The current standard says in 6.1:

A variable is introduced by the declaration of a reference [...] or of an object.

The standard is careful not to say "created" because the point of creation at run time will be different than this "introduction", especially for references. The name in the static code is available after the declaration; the object is available at some time during the run time. In the case of a reference, the object may actually be gone but the variable may still be in scope (for example when you return a reference to a local variable from a function).

We see:

  • A variable may name an object:
    int i;
  • A variable may be a reference:
    int i; int &ri{i};
  • Since there are dangling references, there are variables without objects:
    int &f(){int i; return i;} int &r = f();

It continues

The variable’s name, if any, denotes the reference or object.

  • OK: I suppose this constitutes a variable without name:
    int &f(int /*unused*/) {}

But not all unnamed objects are variables:
int *p = new int{};

p is, of course, a variable; the int it points to is not, even though it is an object.

Of course there are, if you are a friend of oxymorons, constant variables. There are also names which are not variables, for example labels.


You are asking actually two questions. The title is "Can an instance of a struct or a class be considered as a variable?" The answer is that an instance in itself is not a variable; it is just an object. You can create one with new as with the int in the example above. That would be an instance but not a variable (if you assign the address to a pointer, the pointer would a variable containing an address).

In the question body you ask something different: "Can we consider the name of an instance of a struct or a class as a variable?" I would say that is correct: A variable is (normally) a named object. By the way, whether it's a struct or a built-in type is unimportant here.

Upvotes: 9

Miles Budnek
Miles Budnek

Reputation: 30619

The C++ standard defines a variable as such (from [basic.pre]):

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.

So a variable is either:

  1. A reference
  2. An object

That means that a type name is not a variable, but an object is (be it of a class type or a primitive type).

Upvotes: 3

Related Questions