O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conversion Operators

What happens when you try to assign a variable of a built-in type, such as int or unsigned short, to an object of a user-defined class? Listing 14.7 brings back the Counter class and attempts to assign a variable of type int to a Counter object.

Listing 14.7 will not compile!

Listing 14.7. Attempting to Assign a Counter to an int
 0: // Listing 14.7 1: // This code won't compile! 2: #include <iostream> 3: 4: class Counter 5: { 6: public: 7: Counter(); 8: ~Counter(){} 9: int GetItsVal()const { return itsVal; } 10: void SetItsVal(int x) {itsVal = x; } 11: private: 12: int itsVal; 13: }; 14: 15: Counter::Counter(): 16: itsVal(0) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required