Couple of points,
1. In normal circumstances the compiler automatically generates a bitwise copy constructor if none is present. This can be verified easily. Note that in this case, the copy ctor is declared but not implemented which means the compiler will not create a default copy ctor. (Myers explains this very well :).
2. explicit constructors are used to prevent the compiler from converting a basic type to an object, if indeed such a conversion is possible. For example, class A that has a constructor that receives an integer would cause a constructor call in the following code:
A a = 5;
Why is this ? Even before the assignment operator function is called you have to understand that the result of the expression should be an object of class A. Therefore, the 5 has to be converted to an A. If a trivial manner to do so is possible (as in this case, the ctor only needs an int), the compiler will generate a temporary object and then call the assignment operator.
Using an explicit constructor is telling the compiler that he can't perform the conversion. It's like std::string and char *. If the ctor was explicit you wouldn't be able to use code like: string s1 = "abc";
Explicit ctor means that YOU have to perform the construction yourself (in this case coversion).
In order for this code to work you either have to use the proxy method, remove the explicit declaration from the ctor or create an object yourself (not a temporary one).
Eli
Shachar Shemesh wrote:
Hi all,
Here is a small program for your viewing pleasure:
class a { public: explicit a(int param);
a &operator= ( a &that ); };
int main() { a var1(3);
var1=a(5);
return 0; }
================================================================= To unsubscribe, send mail to [EMAIL PROTECTED] with the word "unsubscribe" in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
