1

Take this for example:

const Integer operator+(const Integer& left, const Integer& right) {
  return Integer(left.i + right.i); 
}

(taken from page 496 of Thinking in C++)

What is the part after the return statement? A cast (to make the result of the sum an Integer) or a call to the constructor of the class Integer? Or maybe something else that I ignore..

This is the constructor:

Integer(long ll = 0) : i(ll) {}

Edit:

i it's long int.

Overflowh
  • 1,103
  • 6
  • 18
  • 40
  • It is called an *explicit type conversion (functional notation)*. This notation could signify a constructor call, a call to a user-defined type conversion operator, or a built-in conversion, depending on what the source and the target types are. In your case this is a constructor call. – n. m. could be an AI May 11 '13 at 17:57
  • You can always see if the constructor is used by adding an output statement inside of it. – stefan May 11 '13 at 17:58

2 Answers2

4

Casting means "changing an entity of one data type into another". That said, you can consider Integer() as a cast from long to Integer, as the two types are related and the operation translates into "build an object of type B, starting with an object of type A".

With this syntax, there is no protection against misuse, i.e. if the constructor takes only one parameter, the parameter might not be used to build an object directly representing the first (e.g. each QWidget takes a pointer to the parent, but it is not representing its parent, obviously), and you cannot do anything to prevent this. You could block implicit initialization by marking single-parameter constructor as explicit, but nothing more.

The syntax for old-style casts and constructors with only one parameter is exactly the same, and that's the reason why a new syntax was created for the first: use new style (explicit) C++ syntax for casts, that is, const_cast, dynamic_cast, static_cast or reinterpret_cast as appropriate.

In the very words of Bjarne Stroustrup, this verbose casting syntax was introduced to make clear when a cast is taking place. Note that having four forms also allows for proper differentiation of the programmer's intent.

Finally, int() and such are considered old-style for plain types (int, long, etc.) and newvar = (T)oldvar form exists only because of C compatibility constraint.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • 1
    "int() and such is considered old-style" — NOT. – n. m. could be an AI May 11 '13 at 17:58
  • 2
    @n.m. You should start backing up your statements by facts. Here are some for Stefanos statement: http://stackoverflow.com/questions/1609163/what-is-the-difference-between-static-cast-and-c-style-casting – stefan May 11 '13 at 18:00
  • 1
    @stefan: `type(value)` is not a C-style cast. `(type)value` is. The linked statement only talks about C-style casts, I talk only about the other kind. Any other attempts at backing that up? – n. m. could be an AI May 11 '13 at 18:06
  • 2
    @n.m. You're right, my apologies, I apparently didn't read carefully enough. Still, you could have said why you disagree with that statement in the first place, rather than just writing "NOT". – stefan May 11 '13 at 18:12
  • In what way does `int(x)` differ from `(int)x`? It is syntactical sugar, it was only introduced to offer uniform handling between plain types and objects. The same reasonings apply. Anyway, if @stefan has some valid source to provide, I'm ready to change my answer. – Stefano Sanfilippo May 11 '13 at 18:22
  • @StefanoSanfilippo: it makes little sense to use `static_cast` for user-defined conversions. You *could* do that, but is it really considered good style nowadays? – n. m. could be an AI May 11 '13 at 18:28
  • @StefanoSanfilippo: if you are only talking about built-in types, then I agree, and my apologies. – n. m. could be an AI May 11 '13 at 18:34
  • 1
    Yes, I was just about to write about the clarification I made to the answer. I thought it was clear, because I had discussed the object-to-object and pod-to-object in the first part. Anyway, [this](http://codepad.org/MI20uxhe) is a counter-example: the first conversion (which will probably lead to a bug) takes places, while the second triggers an error. – Stefano Sanfilippo May 11 '13 at 18:38
  • @n.m. I do use `static_cast` to explicitly spell a conversion, or otherwise use uniform initialization for C++11 (so `target { source }`). Never parentheses. This answer is very good advice. – Luc Danton May 12 '13 at 00:43
  • Hi Stefano, I went ahead in reading the book and I found this: "`return Integer(left.i + right.i);` _This may look at first like a 'function call to a constructor', but it’s not. The syntax is that of a temporary object; the statement says 'make a temporary Integer object and return it'. Because of this, you might think that the result is the same as creating a named local object and returning that. However, it’s quite different._" But Bruce doesn't talk about cast. Is it the same thing with different words? – Overflowh May 13 '13 at 11:16
0

Its a constructor call.

Object creation in c++ will be in 2 ways,

Integer* i = new Integer(args); //A Pointer i , pointing to the object created at a memory location.

or

Integer i = Integer(args); //Object i

Your case is 2nd one, but the initialized object is not assigned to i. Rather it is passed as it is.

Moreover, A cast could be trivial if it is (DataType) value., In this case it would be surely a cast.

But in the case of DataType(value) if it is a primitive type, it would be a cast, but if it is a non-primitive type surely it will be a constructor call.

Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77