C++ Constructor Arcane-ness :)

Started by
12 comments, last by discman1028 18 years, 1 month ago
Quote:I would have been surprised if that compiled, since no constructor is made for type double!


There is an implicit promotion from int to double. This code should compile.

I did not know that was possible. I would be less surprised, in this shown case though, since I can understand implicit casting from int to double.

Quote:But from a POD (plain old datatype) to an object, via a constructor, I was surprised.


Single parameter constructors are known as conversion constructors. They are the counterpart of conversion operators.

Quote:By the way, I believe this property may only be "nested" a couple times (only a couple constructor levels down).


The implicit conversion chain can only contain one user-defined conversion.

Quote:@Anonymous Poster: Yep, that 'explicit' keyword is good to know! Thanks to the posters here for letting me know about it!


Single-parameter constructors should be marked explicit unless you know you want them to act as conversion constructors.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Quote:Original post by discman1028
I can understand implicit casting from int to double. But from a POD (plain old datatype) to an object, via a constructor, I was surprised.


There is no implicit cast from int to double. There is an implicit type conversion, though. A cast and a type conversion are two different things. Perhaps that's why you're surprized.

One of the guiding principals in C++ is "what would int do?" (WWID). Okay, so maybe I just made up that expression, but the pricipal is there. In the example I gave, the int is automatically converted to a double. It would be the same if there was a conversion constructor for a non-pod type. Do what the ints do.

The overload resolution rules are, in order:

(1) exact match

(2) match using a promotion (eg. bool to int, flot to double, etc)

(3) match using a standard conversion (eg. int to double, Derived* to Base*, etc)

(4) match using user-defined conversion (conversion constructor, cast operator())

(5) match using the ellipsis


More than one match at the same level is an ambiguity error.

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Bregma
"what would int do?" (WWID)


I believe "Do as the ints do" is more common, but yours is certainly more amusing [grin]
Quote:Original post by Bregma
Quote:Original post by discman1028
I can understand implicit casting from int to double. But from a POD (plain old datatype) to an object, via a constructor, I was surprised.


There is no implicit cast from int to double. There is an implicit type conversion, though. A cast and a type conversion are two different things. Perhaps that's why you're surprized.


Nope that's not why, I meant implicit conversion, but used "cast" lazily. Just surprized b/c I've never seen it before.

Quote:Original post by Fruny
Quote:
I would have been surprised if that compiled, since no constructor is made for type double!



There is an implicit promotion from int to double. This code should compile.


Let's just say, I would have been surprised 48 hours ago...


Thanks all!
--== discman1028 ==--

This topic is closed to new replies.

Advertisement