#ifndef CONVERSIONCLASS_H #define CONVERSIONCLASS_H #include #include using namespace std; //-------------------------------------------------------------------- // A class whose only purpose is to let us experiment with operator // and conversion overloading. //-------------------------------------------------------------------- class MyClass { private: int _val; static const map _string_map; // Some class-owned, helper data public: //--------------------------------------------------------------- // This constructor allows C++ to perform implicit conversions // of integers to MyClass objects. //--------------------------------------------------------------- MyClass(int i) { cout << "MyClass(" << i << ")" << endl; _val = i; } //--------------------------------------------------------------- // This constructor allows implicit conversion from a // string to a MyClass. (This is a very artificial idea!) //--------------------------------------------------------------- MyClass(const char * s) { cout << "MyClass(" << s << ")" << endl; auto it = _string_map.find(s); if ( it != _string_map.end() ) _val = it->second; else _val = 0; } //--------------------------------------------------------------- // This addition operator requires a MyClass object on the // left-hand-side (lhs) and another on the right hand side (rhs). // C++ will do an implicit conversion of an int to a MyClass // for the rhs argument, though, because of the constructor we // provided, and similarly for a string. // The argument must be 'const' for these implicit conversions to take // place. //--------------------------------------------------------------- MyClass operator+(const MyClass &other) const { cout << "MyClass(" << _val << ") + MyClass(" << other._val << ")" << endl; return MyClass(_val + other._val); } //--------------------------------------------------------------- // We can't use a class method when a non-MyClass is on the lhs, // so we use a regular old method. But, we want that method to have // access to the private state of the MyClass object. Thus, 'friend'. //--------------------------------------------------------------- // Example use: 4 + myClassObj friend MyClass operator+(int lhs, const MyClass& rhs); // Example use: cout << ... << myClassObj << ... friend ostream& operator<<(ostream& os, const MyClass& t); //--------------------------------------------------------------- // You can overload conversion operators in C++. Usually you // do this to enable implicit conversions between primitive types and objects // of your class. In this not-too-unusual case, simply enabling an implicit // conversion to int causes an ambiguity: when confronted with // cout << myClassObj, // the compiler doens't know whether to use the overloaded << operator // above, or to implicitly convert myClassObj to an int and then apply // the << operator. To fix that, we modify this method with the 'explicit' // keyword, disabling implicit conversion using it, but still allowing // explicit conversion. //--------------------------------------------------------------- explicit operator int() const { return _val; } }; #endif // CONVERSIONCLASS_H