類型轉換

類型轉換
類對象和其餘類型對象的轉換
轉換場合有:
                  ——賦值轉換
                  ——表達式中的轉換
                  ——顯式轉換
                  ——函數調用,傳遞參數時的轉換
轉換方向有:
                  ——由定義類向其餘類型的轉換
                  ——由其餘類型向定義類的轉換

一、 由其餘類型(如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;
}
相關文章
相關標籤/搜索