Conversion Operators in C++

Viewed 56211

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;
}
4 Answers
Related