2

Are simple type variables objects in C++?

int x = 0;

Obviously, that depends on the definition of the object. So would x be object in this case? It may not be defined by the standard in any way, but please do claim so if you are sure that this is either object or it is not.

Leonid
  • 19,596
  • 22
  • 62
  • 88

3 Answers3

10

Yes (object in the standard are simply a region of storage -- 1.8/1)

An object is a region of storage. [Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. ] An object is created by a definition, by a new-expression_ or by the implementation when needed.

And for variable (3/4)

A name is a use of an identifier that denotes an entity or label. A variable is introduced by the declaration of an object. The variable's name denotes the object.

For the curious, the definition is inherited from C (with modifications needed for C++). The C 90 Standard states:

object: A region of data storage in the execution environment, the contents of which can represent values.

AProgrammer
  • 48,232
  • 8
  • 83
  • 139
2

From 1.8.1

-1- The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage. [Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. ] An object is created by a definition (basic.def), by a new-expression (expr.new) or by the implementation (class.temporary) when needed. The properties of an object are determined when the object is created. An object can have a name (clause basic). An object has a storage duration (basic.stc) which influences its lifetime (basic.life). An object has a type (basic.types). The term object type refers to the type with which the object is created.

It's also implicit in this statement from 3.9.1:

Types describe objects (intro.object), references (dcl.ref), or functions (dcl.fct).

Obviously, for "objects" to group everything that's typed other than references or functions, it must include int et al.

It's worth noting that basic types like int are not objects in the "Object Oriented" sense, but that's pretty obvious from the fact that they're the same as in C which is not an Object Oriented language.

Tony Delroy
  • 94,554
  • 11
  • 158
  • 229
0

Yes, it is. According to §7/5:

If the decl-specifier-seq contains the typedef specifier, the declaration is called a typedef declaration and the name of each init-declarator is declared to be a typedef-name, synonymous with its associated type (7.1.3). If the decl-specifier-seq contains no typedef specifier, the declaration is called a function declaration if the type associated with the name is a function type (8.3.5) and an object declaration otherwise.

So, what you have there is an object declaration (that also happens to be an object definition).

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035