Reputation: 181
I want to know wether the 'int' is defined as a class , means like somewhere in some header file their is a class named int
class int
{
}
And when we declare a variable , an instance of class int is created ? If 'int' is a class , in which file it is stored??
Upvotes: 0
Views: 213
Reputation: 101456
int
is not a class, its a native type.
When you declare an int:
int x;
You do create an instance of int
.
C++ is different than many other languages in this respect where, like in Java or Ruby for instance, "everything is an object." This generally means that everything is derived from one root class, or at least appears to be.
Consider for example Ruby, where everything is ultimately derived from Object
. Object
, in turn, is implemented as a fully-fledged class. It has methods on it like to_s
and code that implements those methods.
C++ isn't like that. C++ has very basic types, like int
, that aren't derived from anything. There's no code behind these types, and they have no methods on them. You can't do something like this:
int x = 42;
string s = x.to_s();
because there's no to_s()
method on an int
, or any methods.
You also asked,
If 'int' is a class , in which file it is stored??
int
isn't "stored" in any file. The meaning and definition of an int
is built in to the compiler itself. There's no file that you can open on your machine to see how an int
is defined. When you do something like:
int x = 42;
x += 77;
...the compiler doesn't have to look in any header file to know how to add 77 to 42. It already knows, because and int
is something that the compiler already knows about. Almost like a-priori knowledge, it's "just there".
How does the compiler already know? Because the people who wrote the compiler (probably using C or C++, by the way) coded that knowledge in. How did the compiler writes know what to write? They followed a document, called the C++ Standard, which explains exactly how a conformant compiler should behave.
Upvotes: 1
Reputation: 258578
No, int
is a fundamental type, as defined in:
2) There are five signed integer types : “
signed char
”, “short int
”, “int
”, “long int
”., and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment); the other signed integer types are provided to meet special needs.
Upvotes: 0
Reputation: 477010
No; int
is not a type of "class-type". It is a so-called "scalar" type.
To determine more generally whether a given object type T
is of scalar, array, union or class type, you can #include <type_traits>
and use std::is_scalar<T>::value
, and similarly for the traits is_array
, is_union
and is_class
.
(Note further that not all types are object types; there are also reference types and function types. You can use std::is_object
and is_function
and is_reference
to make that distincition first.)
Upvotes: 4
Reputation: 8831
No, int
is a built-in type. It is not a class. The compiler has built-in knowledge of how to deal with int
.
Upvotes: 0