类型转换

类型转换
类对象和其余类型对象的转换
转换场合有:
                  ——赋值转换
                  ——表达式中的转换
                  ——显式转换
                  ——函数调用,传递参数时的转换
转换方向有:
                  ——由定义类向其余类型的转换
                  ——由其余类型向定义类的转换

一、 由其余类型(如int、double)等向自定义类的转换是由构造函数来实现的 ,只有当类的定义和实现中提供了合适的构造函数时,转换才能经过。
eg: 假设由int类型向自定义point类转换
      ——point类的定义和实现中给出了仅包括只有一个int类型参数的构造函数 Point pt1 = 5;
      ——point类的定义和实现中给出了包含一个int类型参数,且其余参数都有缺省值的构造函数
      ——point类的定义和实现中虽然不包含int类型参数,但包含一个非int类型参数如float类型,此外没有其余参数或者其余参数都有缺省值,且int类型参数可隐式转换为float类型参数。

二、在构造函数前加上关键字 explicit 能够关闭隐式类型转换

#include <iostream>
using namespace std;
class AnotherPoint
{
        private:
                double _ax;
                double _ay;
        public:
                //explicit
                AnotherPoint(double x=0,double y=0):_ax(x),_ay(y)
                {
                        cout<<"AnotherPoint(int,int)"<<endl;
                }
                friend std::ostream & operator << (std::ostream & os,const AnotherPoint &rhs);
                friend class Point;
};
std::ostream & operator << (std::ostream & os,const AnotherPoint & rhs)
{
        os<<"("<<rhs._ax<<","<<rhs._ay<<")";
        return os;
}

class Point
{
        private:
                int _x;
                int _y;
        public:
                //explicit
                Point(int x=0,int y=0):_x(x),_y(y)
                {
                        cout<<"Point(int,int)"<<endl;
                }
                //explicit
                Point(AnotherPoint & a):_x(a._ax),_y(a._ay)
                {
                        cout<<"Point(AnotherPoint & a)"<<endl;
                }
//point& operator=(const anotherPoint& rhs);   // 赋值运算符只能两个相同类型的类对象之间 
                friend std::ostream & operator << (std::ostream & os,const Point & rhs);
};
std::ostream & operator << (std::ostream & os,const Point & rhs)
{
        os<<"("<<rhs._x<<","<<rhs._y<<")";
        return os;
}
//由其它类型向自定义类型进行转换,都是经过构造函数来完成的
//隐式转换
//explicit能够禁止隐式转换
int main(void)
{
        Point p1;
        cout<<5<<"转换成:";
        p1=5;      //类型不一样,因此构造函数转换
//若是有operator=这就就报错了
        cout<<" p1 = "<<p1<<endl<<endl;

        double d1=1.5;
        cout<<d1<<"转换成:";
        p1=d1;
        cout<<" p1 = "<<p1<<endl<<endl;

        AnotherPoint p2(12.34,56.78);
        cout<<" p2 = "<<p2<<endl;
        cout<<"将p2转换成p1"<<endl;
        p1=p2;     //两个都是类类型,因此直接赋值构造函数
        cout<<" p1 = "<<p1<<endl<<endl;
        return 0;
}



类型转换函数
一、 能够经过 operator int() 这种相似操做符重载函数的类型转换函数 自定义类型向其余类型的转换。如将point类转换成int类型等。

二、 在类中定义类型转换函数的形式通常为:
              operator 目标类型名();
三、有如下几个使用要点:
     ——转换函数 必须是成员函数 ,不能是友元形式。
     ——转换函数 不能指定返回类型 ,但在函数体内 必须用return语句以传值方式返回一个目标类型的变量
     ——转换函数 不能有参数

#include <iostream>
using namespace std;
class AnotherPoint
{
        private:
                double _ax;
                double _ay;
        public:
                AnotherPoint(double x=0,double y=0):_ax(x),_ay(y)
                {
                        cout<<"AnotherPoint(double,double)"<<endl;
                }
                friend ostream & operator << (ostream & os,const AnotherPoint & rhs);
};

class Point
{
        private:
                int _x;
                int _y;
        public:
                Point(int x=0,int y=0):_x(x),_y(y)
                {
                        cout<<"Point(int,int)"<<endl;
                }
//类型转换函数
                operator int()
                {
                        return _x;
                }
                operator double()
                {
                        return _x*_y;
                }
                operator AnotherPoint()
                {
                        return AnotherPoint(_x,_y);  
 // 这里直接返回类类型对象,因此不能单单AnotherPoint类前向声明,必须给出完整定义
                }
                friend ostream & operator << (ostream &os,const Point & rhs);
};
ostream& operator << (ostream& os,const AnotherPoint & rhs)
{
        os<<"("<<rhs._ax<<","<<rhs._ay<<")";
        return os;
}
ostream& operator << (ostream& os,const Point & rhs)
{
        os<<"("<<rhs._x<<","<<rhs._y<<")";
        return os;
}

int main()
{
        Point p(4,5);
        int x=p;
        cout<<"x = "<<x<<endl;
        double y=p;
        cout<<"y = "<<y<<endl;
        AnotherPoint p1;
        cout<<"p1 = "<<p1<<endl;
        p1=p;
        cout<<"p1 = "<<p1<<endl;
        return 0;
}
相关文章
相关标签/搜索