D
D
Daniil Demidko2016-01-26 10:54:05
C++ / C#
Daniil Demidko, 2016-01-26 10:54:05

What is the difference between int a() and int a=int()?

I know it sounds silly and I wouldn't be asking if I hadn't experimented in the console

int first (); //  first == 1
int two = int (); // two == 0

But why is that?
For non-primitive types AnyType anyObject (), what AnyType anyObject = AnyType ()is a constructor call, but with primitives?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MiiNiPaa, 2016-01-26
@Daniro_San

Now print typeid(first).name() to the screen and be surprised.
The first line is the declaration of the function first without parameters and returning an int. The function name is converted to a pointer, the pointer is converted to bool(true) which is converted to int(1).

V
Vasily, 2016-01-26
@Foolleren

implicit type casting or how to shoot yourself in the foot in C and C++

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question