Copy Const vs Assignment Operator

1
2
3
4
5
6
7
1)
X x;
Y y(x);  //explicit conversion

2)
X x;
Y y=x;//implicit conversion 


How does 2) does an implicit converstion? What are the steps executed by compiler?

Thanks!
2) is identical to 1).
In 1) you call the copy constructor with explicit syntax, in 2) you call the same constructor with implicit syntax
closed account (1yR4jE8b)
yeah, both are the same...just different ways to write them.

I try to be explicit as much as possible, I've had some weird things happen with implicit conversions.
So,

1) if I don't have an explicit Copy Const, could 1) compile (with just an implicit Copy Const)?

2) I think that 2) won't compile if there is a one and only one Copy Const declared, which is explicit. Is that true?
Last edited on
If you declare the constructor explicit you can call it only with ()
If you don't you can use =

There's absolutely no difference when using = instead of () for constructors
Thanks Bazzy!

Do you know why the auto_ptr class have only explicit constructor, and not implicit at all? Wouldn't adding an implicit constructor facilitate initialization as in:

Y y=x

?
The point is to force the programmer to know what they are doing. Otherwise, think about
it -- EVERY single raw pointer you hold in your program could be implicitly convertible to
a std::auto_ptr.
Thank you!
Topic archived. No new replies allowed.