3

From what I understand it is legal to instantiate an integer in c++ like this:

int x = int(5);

As a Java programmer I would assume that this line of code calls the constructor of the integer passing "5" as an argument. I read though that int is not a class and thus has no constructor.

So, what does exactly happen in that line of code and what is the fundamental difference between initialising the int that way and this way:

int x = 5;

Thanks in advance!

legends2k
  • 31,634
  • 25
  • 118
  • 222
Tomasito665
  • 1,188
  • 1
  • 12
  • 24
  • 4
    Well it casts `5` to an `int`. Which does nothing at all. – BoBTFish Dec 16 '15 at 14:07
  • 10
    `int(5)` is a [function cast expression](http://en.cppreference.com/w/cpp/language/explicit_cast). It is not specific to initialization and can in fact appear anywhere an expression is allowed. – n. m. could be an AI Dec 16 '15 at 14:09
  • 1
    @BoBTFish: No, casting is data type identifier in parentheses, followed by expression. – Pavel Pája Halbich Dec 16 '15 at 14:10
  • 2
    @PavelPájaHalbich C++ is not C. You are allowed to cast by placing parentheses around the argument. – PaulMcKenzie Dec 16 '15 at 14:13
  • Related (Dupe?): [Do built-in types have default constructors?](http://stackoverflow.com/q/5113365/183120) – legends2k Dec 16 '15 at 14:14
  • 1
    If `int(5)` or similar constructs were not allowed, a whole lot of templated code would fail to work. – PaulMcKenzie Dec 16 '15 at 14:15
  • @n.m. Thanks. My bad. – unwind Dec 16 '15 at 14:15
  • 6
    *As a Java programmer I would assume* -- Maybe that's the real issue. C++ is not Java, so assumptions how C++ works by using Java as a model will only get you into trouble. – PaulMcKenzie Dec 16 '15 at 14:17
  • @n.m. Well, OK. I develop mainly in C#, although sometimes I code in C++ as well, when I'm working on games. In a view of a compiler, how does compiler knows that is cast and not a call of implicitly declared constructor? – Pavel Pája Halbich Dec 16 '15 at 14:19
  • 1
    Aaah. I understand. So int(x) does not refer to an constructor, but to one of the possible ways to cast in C++. Can somebody place it as an answer so that I can accept it and close the question? – Tomasito665 Dec 16 '15 at 14:21
  • @PavelPájaHalbich Built-in types have no constructor, so no confusion there. For classes, it'd try to call the respective constructor. – legends2k Dec 16 '15 at 14:21
  • @legends2k So this syntax would not be possible to cast a not build-in type, right? – Tomasito665 Dec 16 '15 at 14:23
  • It is legal way to initialize, but not recommended if your compiler supports C++11. Check {} initialization, like ' int x{5};' – StahlRat Dec 16 '15 at 14:29
  • You really should read. "The functional cast expression [..] is exactly equivalent to the corresponding C-style cast expression." – Karoly Horvath Dec 16 '15 at 14:29
  • @Tomasito665 Yes. The compiler would look for a constructor that takes a single argument of the right type or a [user-defined conversion function](http://en.cppreference.com/w/cpp/language/cast_operator). – legends2k Dec 16 '15 at 14:30

2 Answers2

12

I read though that int is not a class and thus has no constructor.

Yes, technically built-in types have no constructor.

what does exactly happen in that line of code

The integer literal 5 is explicitly cast to an int (a no-op for the compiler mostly) and assigned to x.

what is the fundamental difference between initialising the int that way and int x = 5;

Essentially, no difference. All the below expressions are the same in most cases, unless you're a language lawyer (for instance, the last would prevent narrowing i.e. raise an error if the value cannot be represented by the type):

int x = 5;         // copy initialization
int y = int(5);    // cast and copy initialization
int z = (int)5;    // cast and copy initialization
int w(5);          // direct initialization
int r{5};          // direct initialization

Read more about initializations for finer details and differences.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
2

C++ and java work differently when it comes to types. While java (to my understanding) uses basic built in types and reference types, C++ uses a different type system.

C++ built in types includes fundamental types (bool, character types like char, integer types, floating point types, and void) as well as some other types such as reference types like double& or std::vector<std::sting>&& and pointer types.

In addition to those, C++ supports user defined types (structs, classes, enum and enum class). the standard library provides many user defined types such as std::string.

it turns out the int a(5); notation is NOT reserved for user defined types only, The ,language supports value initialization that way. in C++11 it is also legal to say int a{5};

now about value assignment:

int a; //declaration
a=5;   //assignment

int b(5); //declaration+initialization
int c=5;  //declaration+initialization (NO assignment)

If a variable has not been declared, there is no assignment, the compiler parses int c=5; just like int c(5);

ForeverStudent
  • 2,487
  • 1
  • 14
  • 33