Please help me understand how exactly the conversion operators in C++ work. I have a simple example here which I am trying to understand, though it is not very clear how the conversion actually happens by the compiler.
class Example{
public:
Example();
Example(int val);
operator unsigned int();
~Example(){}
private:
int itsVal;
};
Example::Example():itsVal(0){}
Example::Example(int val):itsVal(val){}
Example::operator unsigned int (){
return (itsVal);
}
int main(){
int theInt = 5;
Example exObject = theInt; // here
Example ctr(5);
int theInt1 = ctr; // here
return 0;
}
If you compiler supports copy constructor optimization and return value optimization you won't notice
execution, but you can declare copy constructor to be private to understand what I am talking about.
At statement 1 the constructor
Example(int val)
is called. Declare it asexplicit Example(int val)
and you will get a compile time error i.e. no implicit conversion will then be allowed for this constructor.All single argument constructors are called implicitly if the assigned value is of their respective argument type. Using the
explicit
keyword before single argument constructors disables implicit constructor calling and hence implicit conversion.If the constructor was declared as explicit i.e.
explicit Example(int val)
then the following would happen for each statement.Also note that in case of implicit constructor call and hence implicit conversion the assigned value is an rvalue i.e. an un-named object implicitly created using an lvalue (theInt) which tells us that in case of implicit conversion the compiler converts
to
So (in C++11) don't expect the lvalue constructor to be called seeing that you are using an lvalue i.e. a named value
theInt
for assignment. What gets called is the rvalue constructor since the assigned value is actually the un-named object created using the lvalue. However, this applies if you have both lvalue and rvalue versions of the constructor.At statement 2
operator unsigned int()
is called. Simply consider it as a normal function call with a weird name and the fact that it can get called automagically when an implicit conversion happens. The value returned by that function is the value assigned in the expression. And since in you implementation the value returned is an int it correctly gets assigned toint theInt1
.To be precise
operator unsigned int()
overloads()
operator which is the cast operator. In your case it's overloaded forint
hence whenever an object ofExample
class is assigned to anint
the implicit type casting fromExample
toint
takes place and henceoperator unsigned int()
gets called. Therefore,is equivalent to
This uses implicit conversion of int to Example, effected by the non-explicit constructor which accepts an int.
This also requires the availability of copy constructor for Example, even though the compiler is allowed to omit copying the instance.
This uses implicit conversion of Example to unsigned int, provided by the cast operator.
Cast operators are normally avoided, since they tend to lead to confusing code, and you can mark single-argument constructors explicit, to disable implicit conversions to your class type. C++0x should add also the possibility to mark conversion operators explicit (so you'd need a static_cast to invoke them? - my compiler doesn't support them and all web resources seem to be concentrating on explicit conversion to bool).
You can walk through that code with a debugger (and/or put a breakpoint on each of your constructors and operators) to see which of your constructors and operators is being invoked by which lines.
Because you didn't define them explicitly, the compiler also created a hidden/default copy constructor and assignment operator for your class. You can define these explicitly (as follows) if you want to use a debugger to see where/when they are being called.