看到《深度探索
C++
对象模型》的第
21
页,在其谈到
C struct
在
C++
中的一个合理用途时,看到了
C++
中用户定义的转换的运用。因而回头将《
C++ Primer
》中关于用户定义的转换的一些细节温习了一下。
(1)
用户定义的转换的必要性:
我们希望能够在
SmallInt
对象和其他
SmallInt
对象或者内置算术类型的对象之间进行加减操作,我们要通过提供
6
个
SmallInt
操作符函数来实现对这些操作的支持:
class
SmallInt
...
{
friend operator+( const SmallInt &, int );
friend operator-( const SmallInt &, int );
friend operator-( int, const SmallInt & );
friend operator+( int, const SmallInt & );
public:
friend operator+( const SmallInt &, int );
friend operator-( const SmallInt &, int );
friend operator-( int, const SmallInt & );
friend operator+( int, const SmallInt & );
public: