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.