Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it technically correct to say variables and constants are objects of data types?

Tags:

c++

c

Can one say Variables and constants are objects of data types ?

I wonder what would be the proper explanation for this

int a;

float f;

Here, Can we say a is an object of type int and f is an object of type float?

like image 238
Rak Avatar asked Apr 27 '13 10:04

Rak


People also ask

What are variables constants and data types?

A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant. A variable is a data item whose value can change during the program's execution. Thus, as its name implies – the value can vary.

Is a constant a data type?

A constant is a data object with a fixed value that cannot be changed during program execution. The value of a constant can be a numeric value, a logical value, or a character string. A constant that does not have a name is a literal constant. A literal constant must be of intrinsic type and it cannot be array-valued.

What is the term used to indicate the variable and constants of a class?

d) Constants. Explanation: The variables inside a class are termed data members of the class.


2 Answers

Yes

Per paragraph §1.8, both a and b are objects of their corresponding types.

1An 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. —end note ] An object is created by a definition (3.1), by a new-expression (5.3.4) or by the implementation (12.2) when needed. The properties of an object are determined when the object is created. An object can have a name (Clause 3). An object has a storage duration (3.7) which influences its lifetime (3.8). An object has a type (3.9). The term object type refers to the type with which the object is created.

[intro.object]

and those variables fit in the above quoted definition.

like image 135
masoud Avatar answered Nov 11 '22 06:11

masoud


a and f are objects of type int and type float, respectively. Yes, that contradicts what @Patashu says, and that's because we're using different definitions of "object".

@Patashu uses the definition from object-oriented programming: an object is a thing with methods, etc. And that's perfectly fine.

However, C++ is a multi-paradigm language -- it supports more than one programming model. The C++ language definition uses the word "object" in the broader sense that compiler writers use: an object is a region of storage with various operations that can be performed on that storage. The operations are defined by the object's type. There's a well-defined set of operations that can be applied to an object of type int, so when you know that you're dealing with an int you and the compiler know what things you can do with it and, by implication, what things you can't do with it.

like image 26
Pete Becker Avatar answered Nov 11 '22 05:11

Pete Becker