Search code examples
c++variables

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


I've read:

  • A variable is a name of memory location.
  • In C++, a variable is actually just a bit of memory that has been reserved for the program’s use. (https://en.cppreference.com/book/intro/variables)
  • Variable names are identifiers. Identifiers are labels used for various things in C++, including functions, classes, and namespaces as well as variables.

(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.


Solution

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