 )
)class SmallInt
{
public:
    SmallInt(int i=0):val(i)
    {
        if(i<0 || i>255)
            throw std::out_of_range("越界!!!越界进球无效!");
    }
    explicit operator int() const {return val;}  //转换操作符,不能有参数,是成员函数,没有返回类型
private:
    size_t val;
};
void fun1()
{
    SmallInt si;
    si=4;   //吧4隐式转换成SmallInt类型
//    si+3;   //隐式转换si为int类型,由于explicit这里会出错,加了explicit之后只能显式转换
}
void fun2()
{
    int i=42;
//    cin<<i;   this code would be legal if the conversion to bool were not explicit!
}
/**
* 功能:重载,转换,运算符
* 时间:2014年7月19日15:05:21
* 作者:cutter_point
*/
#include<iostream>
#include <stdexcept>
using namespace std;
/**************************************
Conversion Operators
转换操作符
**************************************/
/*
operator type() const
Conversions to
an array or a function type are not permitted.
转换函数必须是成员函数,不能指定返回 类型,必须有一个空的参数列表。
函数通常应 const。
*/
/**
Defining a Class with a Conversion Operator
*/
//定义一个类,表示一个在0到255范围类的一个整数
class SmallInt
{
public:
    SmallInt(int i=0):val(i)
    {
        if(i<0 || i>255)
            throw std::out_of_range("越界!!!越界进球无效!");
    }
    explicit operator int() const {return val;}  //转换操作符,不能有参数,是成员函数,没有返回类型
private:
    size_t val;
};
void fun1()
{
    SmallInt si;
    si=4;   //吧4隐式转换成SmallInt类型
//    si+3;   //隐式转换si为int类型,由于explicit这里会出错,加了explicit之后只能显式转换
}
/**
Conversion Operators Can Yield Suprising Results
重载操作符能产生惊人的结果
*/
void fun2()
{
    int i=42;
//    cin<<i;   this code would be legal if the conversion to bool were not explicit!
}
/**
explicit Conversion Operators
明确转换操作
*/
//说白了就是叫我们使用explicit来使用!
int main()
{
    return 0;
}
原文:http://blog.csdn.net/cutter_point/article/details/38009587