zoukankan      html  css  js  c++  java
  • 类型转换

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

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

    2、在构造函数前加上关键字 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;
    }



    类型转换函数
    1、可以通过 operator int() 这种类似操作符重载函数的类型转换函数自定义类型向其他类型的转换。如将point类转换成int类型等。

    2、在类中定义类型转换函数的形式一般为:
                 operator 目标类型名();
    3、有以下几个使用要点:
         ——转换函数必须是成员函数,不能是友元形式。
         ——转换函数不能指定返回类型,但在函数体内必须用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;
    }


  • 相关阅读:
    SD卡测试
    测试人员可能会遇到的问题
    HDU 1024 Max Sum Plus Plus
    HDU 1176 免费馅饼
    HDU 1257 最少拦截系统
    HDU 1087 Super Jumping! Jumping! Jumping!
    poj 1328 Radar Installation
    poj 1753 Flip Game
    HDU 1003 Max Sum
    HDU 5592 ZYB's Premutation(BestCoder Round #65 C)
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8915658.html
Copyright © 2011-2022 走看看