Is it correct to say that a variable is an instance of a data type?

(that can change its value over the course of the program)

Or is ‘instance of something’ strictly related to classes and objects? If so, how would you define the concept of ‘variable’ when it comes to C++?


As far as I know, it is correct to call values of primitive types instances of that type (see for example “C++ Type System”, from Visual Studio’s manual, but I am still looking for another source), though I believe this term is mostly used when discussing objects.
An object is an instance of a class C if it was built from a constructor defined either in class C or one of its subclasses.

A variable in C++ is not a runtime concept. It exists inside your compiler as a lexical binding between the variable’s name and an abstract (mutable if not const) location. Once a program is compiled, variables are not there anymore. Instead, data is read and written in registers, memory, etc.


Or is ‘instance of something’ strictly related to classes and objects? If so, how would you define the concept of ‘variable’ when it comes to C++?

To put it as concisely as possible for C++

  • A variable is a name (in code) for a location in memory to be determined at runtime.
  • An instance is an object which resides in memory at runtime.

Just to be clear:

  • Not all areas of memory contain objects. For example, some areas of memory may contain ‘garbage’ values.
  • Not all memory storing objects will have been given a name in code. Objects may reside in un-named areas of memory such as a position in an array, or somewhere on the Heap referenced by a pointer.
  • When an area of memory contains garbage, it’s usually either because no object has been initalised at that location, or because some previously existing object has been destroyed.


Well, what does “correct” mean?

If we want to be super-strict about it, we need to distinguish between:

  • The syntax side: expressions have (static) types
  • The semantics side: objects are instances of classes

So then a variable—being an expression—would have a type. Its value—the object that it refers to—would be an instance of some class. And note that the variable’s type and the object’s class are not generally the same thing—the variable’s static type puts an upper bound on the objects’ runtime classes, so that the classes must be a subtype of the class that corresponds to the type.

Do you want to repeat that big mouthful all the time? Heck no. How strictly vs. loosely we want to talk depends on context. So often there’s really no problem with saying that “the variable is an instance of this type.” What’s happening there really is that we’re using the variable and the type metonymically to refer to the object and a superclass of its runtime class.

Unless you want to spend your entire life in purism and sophism … yes.

Every time you keep something that’s uniquely identified an use to forge other uniquely identifiable things you are doing instantiation.

  • A project instantiate into products
  • A source instantiate into executables
  • A template instantiate into types.
  • A type instantiate into variables.

Some of those instantiation may have more accurate names, but in general this works.

Many people reserve “instance” for instances of classes. If you do that then in C++, a variable could be an instance of a class, a reference to an instance, a pointer to an instance, or a primitive type (you’d likely count structs as classes in that context, they are almost completely the same). In Java or Objective-C, variables would never be instances but possibly references to instances.

Forgot: For a short time, a C++ pointer can be a pointer to raw memory, for example “this” before the constructor starts executing. You’d likely not call *this an instance yet at this point.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *