Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between an object and a variable in C++

I am learning C++ using the books listed here. My question is that is there a difference between a variable and an object. Consider the following example:

int i = 0; //i is an object of type int. Or we can say i is a variable of type int
int &refI = i; //refI is a "reference variable" of "reference type `int&`. But here refI is not an object. 

My current understanding is that both of the terms variable and object overlaps to a large extent. But in some contexts like in case of refI above, there can be some differences. In particular, refI is a reference variable of reference type int& and refI is not an object because a reference is an alias for some other object. While i is both an object and a variable.

My question is that am i correctly analyzing the refI case above? If not, what does the standard say about this.

My second question is that, does the standard C++ strictly differentiate between these two terms. If yes, how and where. For example something like,

a variable may be defined as an object with a name. And any object without a name is not a variable.

Here the user says that a variable and object are different.

Edit

I am also asking this question because i am aware that in Python(as it is a dynamically typed language) there is a clear distinction between variables and objects. Does the C++ standard also make such a clear distinction.

like image 657
Anoop Rana Avatar asked Mar 08 '26 11:03

Anoop Rana


1 Answers

Difference between an object and a variable in C++

Variable is a programming language level concept. A variable has a type and it (usually) has a name. A variable can denote an object, or a reference.

There's no concise definition for the meaning of "variable" in the standard nor a section dedicated to them alone, but closest individual rule to specifying its meaning is:

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


Object is a concept in the level of the abstract machine that the language defines. It is mostly specified in the section "Object model [intro.object]" which begins:

[intro.object]

The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition, by a new-expression ([expr.new]), by an operation that implicitly creates objects (see below), when implicitly changing the active member of a union, or when a temporary object is created ([conv.rval], [class.temporary]). An object occupies a region of storage in its period of construction ([class.cdtor]), throughout its lifetime, and in its period of destruction ([class.cdtor]).


 int i = 0; //i is an object of type int. Or we can say i is a variable of type int

i is a variable of type int. The variable's name denotes an object.

int &refI = i; //refI is a "reference variable" of "reference type `int&`. But here refI is not an object. 

refI is a variable of type int&. The variable's name denotes a reference. The reference is bound to the object named by i and can be seen as another name for the same object.

like image 107
eerorika Avatar answered Mar 11 '26 02:03

eerorika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!