C++ 类之间转化 转化构造函数

C++ 类之间转化 转化构造函数

自定义类型-转化构造函数

class 目标类
{
    目标类(const 源类型 & 源类对象引用)
    {
        根据需求完成从源类型到目标类型的转换
    }
}

目标
实现其它类型到本类类型的转化。

原理
转换构造函数,本质是一个构造函数。是只有一个参数的构造函数。如有多
个参数,只能称为构造函数,而不是转换函数。转换构造,强调的是一转一。

应用
用于传参或是作返回。

explicit 关键字

关键字 explicit 可以禁止"单参数构造函数"被用于自动类型转换。
即 explicit 仅用于单参构造(默认参数构成的单参亦算)。

转化多是刻意而为之,以隐式的形式发生,为了示意同正常构造的不同,常用
explicti 关键字修饰,要求在转化时显示的调用其构造器完成转化。

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//转化构造函数的本质,也是构造函数
class Point2D
{
    //友元类
    friend class Point3D;
public:
    Point2D(int x=0, int y=0 )
            :_x(x),_y(y){}
private:
    int _x;
    int _y;
};

class Point3D
{
public:
    Point3D(int x=0, int y=0 ,int z=0)
            :_x(x),_y(y),_z(z){}
    //转换构造函数
    explicit Point3D(const Point2D & d2)
    {
        this->_x = d2._x;
        this->_y = d2._y;
        this->_z = rand()%100;
    }
    void dumpFormat()
    {
        cout<<"("<<_x<<","<<_y<<","<<_z<<")"<<endl;
    }
private:
    int _x;
    int _y;
    int _z;
};

void foo(Point3D d3)
{
    d3.dumpFormat();
}


int main()
{
    srand(time(NULL));
    Point2D  d2(10,100);

    Point3D  d3 =(Point3D) d2;//在这里隐式调用了转换构造函数
    //!转化多是刻意而为之,以隐式的形式发生,为了示意同正常构造的不同,常用
    //!explicti 关键字修饰,要求在转化时显示的调用其构造器完成转化。

    d3.dumpFormat();
    //foo(d2);
    foo((Point3D)d2);
    return 0;
}



```c++
#include <iostream>
using namespace std;
//关键字 explicit 可以禁止"单参数构造函数"被用于自动类型转换
class mystring
{
public:

    explicit mystring(const char* s= nullptr)
    {
        cout<<"    mystring(const char* s= nullptr)"<<endl;
    }
};
int main()
{
    mystring s("qwer"); //显示调用转换构造
    mystring s2 = "qwer"; //隐式调用转换构造,被explicit禁止
    return 0;
}





## 自定义类型-操作符函数转化
转换函数必须是类方法,转换函数无参数,无返回。

```c++
class 源类{
    operator 目标类(void)
    {
        return 目标类构造器(源类实参);
    }
}
//todo 自定义类型-操作符函数转化
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

class Point3D
{
public:
    Point3D(int x=0, int y=0 ,int z=0)
            :_x(x),_y(y),_z(z){}

    void dumpFormat()
    {
        cout<<"("<<_x<<","<<_y<<","<<_z<<")"<<endl;
    }
private:
    int _x;
    int _y;
    int _z;
};


class Point2D
{

public:
    Point2D(int x=0, int y=0 ):_x(x),_y(y){}

    //操作符函数转化
    operator Point3D (void){
        return Point3D(_x,_y,rand()%100);
    }

private:
    int _x;
    int _y;
};




void foo(Point3D d3)
{
    d3.dumpFormat();
}


int main()
{
    srand(time(NULL));
    Point2D  d2(10,100);

    Point3D  d3 = d2;
    d3.dumpFormat();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可能只会写BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值