I am trying to understand if C++ types are classes, like in Python for example.

Otherwise what datatype is the type itself? E.g. int a = 10 what is int? Is just a token label used by the compiler to decided the space to allocate to the variable?

closed as unclear what you're asking by gnat, Deduplicator, Nicol Bolas, Pieter B, 8bittree Oct 31 '17 at 22:56

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    First, you have to decide what are essential traits of a class. Skipping that step is an exercise in futility. – Deduplicator Oct 27 '17 at 11:03
  • 2
    note that C++ has three keywords that are used for defining class types: class, struct and union, and uses the keyword class in situations beyond declaring and defining classes. – Caleth Oct 27 '17 at 11:23
  • 7
    The relationship is: All classes are types but not all types are classes. int is a type, not a class. – Neil Oct 27 '17 at 11:46
  • 3
    C++ does not have a unified type system. There is no base "Object" as there is in Java or C#. Even two user-defined classes are unrelated in that there is no common base type, unlike Java or C#. There is no attempt to make primitive types like int fit into a common class hierarchy, no automatic boxing and unboxing. – Erik Eidt Oct 27 '17 at 12:15
  • @Deduplicator Shall I explain here what is a class in C++? – G M Oct 27 '17 at 13:05

A type for a variable in C++ does three main things. First of all it is used to determine the actual amount of memory for a variable. The second thing it does is to determine the operations that are supported for the variable. The third thing it does is to determine how data is transformed from other types to the type of the variable and whether such a transformation is possible.

So an int which is a built in type has a certain size that an int variable occupies in memory. For a number of historical, backwards compatibility reasons the size of an int will vary between different kinds of hardware. On a 16 bit CPU it may be 16 bits, on a 32 bit CPU it may be 32 bits, and on a 64 bit CPU it may be 64 bits. However it is always signed. If you want to specify the size no matter what the hardware you need to use one of the more specific types such as short or long or long long.

An int variable also has certain operators and operations that can be used with the variable such as the plus operator (+) for addition and the multiplication operator (*) for multiplication. For an int variable these operators are defined to provide specific behaviors just as for a string variable they have different behaviors, e.g. the plus operator does string concatenation.

Finally an int variable has certain transformations that the compiler will generate for you. If you assign a float to an int variable then the compiler will transform the value of the float into an int by truncating any decimal portion of the value. So a floating point value of 3.456 will be truncated to 3 and a floating point value of 0.123 will be truncated to 0.

And the compiler will not allow some types of transformations because those transformations are not defined for an int. If you define a class which does not have a method for transforming the class to an int and then try to assign a variable of that type to an int the compiler will generate an error message.

The word "type" in C++ or C or most languages that use static type variables is a kind of variable classification used by the compiler to help programmers avoid mistakes and to help the compiler allocate memory more efficiently and to generate more efficient machine code.

A type is a way to classify variables. Languages such as JavaScript do not use static type but rather dynamic type. In JavaScript or php the type of a variable will depend on the value last assigned to the variable and the languages do a lot of conversions for you automatically.

In C++, as in C and Java, there are built in types and there are programmer defined types. Variable types such as int or char are built in types also known as Plain Old Data as these types have direct hardware representations.

A programmer can create defined types in C++ in a number of ways. The most common is by using the class keyword or the struct keyword to define a class which can then be used as a type when defining variables or functions.

With the new standards, C++11 and later, there is a new keyword auto which can be used when defining a variable. This is a way to declare a variable's type by using an expression on the right hand side of the assignment operator to generate a value with a particular type and assign both the value and the type to variable being defined. Once this is done, the variable's type can not be changed however using auto can be really helpful.

  • 2
    "Variable types such as int or char are built in types also known as Plain Old Data as these types have direct hardware representations." They are more correctly known as fundamental types. They are POD, but POD represents something else. There can be POD types that don't have "direct hardware representations. And even fundamental types don't have to have "direct hardware representations". – Nicol Bolas Oct 27 '17 at 15:51
  • 1
    short, long, and long long are not guaranteed to have the same size across different environments. The only guarantees are that short is at least as large as signed char, int is at least as large as short and is large enough to contain the range INT_MIN to INT_MAX, long is at least as large as int, and long long is at least as large as long. If you need a fixed size across platforms, you can use int8_t, int32_t, and similar types from the cstdint header, though it's not guaranteed that every platform will have those. Or make a custom type. – 8bittree Oct 27 '17 at 16:34
  • 1
    You forgot one point: The type determines the kind and range of values a variable of that type can have. Imo that’s the most important aspect of them all. – besc Oct 27 '17 at 18:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.