c++自定义类型转换

一:C++的内置类型和用户自定义类型的互相转换

1.内置类型转换为自定义类型。   需要使用转换构造函数。

带单参数的构造函数(基本数据类型=>自定义数据类型)

这类构造函数称为转换构造函数。

class Complex  
{  
public:  
    Complex():ele1(0.0){};
    Complex(double r, double i):ele1(r),ele2(i){};  
    Complex(double r) //  转换构造函数
    {  
        ele1=r;  
        ele2=0;  
    }  
private:  
    double ele1;  
    double ele2;  
};  

void main()
{
    Complex c;
    c=1.2;//调用转换构造函数将1.2转换为Complex类型
}

 

  • 不仅可以将一个标准类型数据转换成类对象,也可以将另一个类的对象转换成转换构造函数所在的类对象。(下面会展开讲自定义类型之间的转换)

  • 如果不想让转换构造函数生效,也就是拒绝其它类型通过转换构造函数转换为本类型,可以在转换构造函数前面加上explicit

  • explicit constructors:
    在你不想隐式转换,以防用户误操作怎么办?
    C++提供了一种抑制构造函数隐式转换的办法,就是在构造函数前面加explicit关键字,再隐式转换就会导致编译失败,但是,显式转换还是可以进行。

2.自定义类型转换为内置类型。   需要使用类型转换函数。

类型转换函数:

operator 类型名( )
        {
               //实现转换的语句
        }

示例代码:

#include <iostream>
using namespace std;
class test {
	int a;
	string b;
public:
	test(int a) {
		this->a = a;
	}
	test(string b) {
		this->b = b;
	}
	//转换为int的类型转换函数
	operator int() {
		return a;
	}
	//转换为string的类型转换函数
	operator string() {
		return b;
	}
};
int main() {
	//test转为int
	test t(1);
	int a = t;
	cout << a << endl;

	//test转为string
	test t1("hello world");
	string str = t1;
	cout << str << endl;
}

 

二:用户自定义类型之间的相互转换。

 现在很多时候,一个对象的类型转换往往需要创建一个无名的临时对象来暂存。(  临时对象可以隐含发生,也可以显式调用用户设置的构造函数。)

 c++中可以通过构造函数,来自定义类型之间的转换。一个构造函数,只要一个参数调用,那么他就设置了一种从参数类型到这个类类型的类型转换。

程序示例:

#include <iostream>
#include <cmath>
using namespace std;
class Point {	        //Point类定义
public:
	Point(int xx = 0, int yy = 0) {
		x = xx;
		y = yy;
	}
	Point(const Point& p);
	int getX() { return x; }
	int getY() { return y; }

private:
	int x, y;
};
Point::Point(const Point& p) {	//复制构造函数的实现
	x = p.x;
	y = p.y;
	cout << "calling the copy constructor of point" << endl;
};
//类的组合
class Line {	//Line类的定义
public:	//外部接口
	Line(Point xp1, Point xp2);
	Line(Line& l);
	double getLen() { return len; }
private:	//私有数据成员
	Point p1, p2;	//Point类的对象p1,p2
	double len;
};
//组合类的构造函数
Line::Line(Point xp1, Point xp2) : p1(xp1), p2(xp2) {
	cout << "Calling constructor of Line" << endl;
	double x = static_cast<double>(p1.getX() - p2.getX());
	double y = static_cast<double>(p1.getY() - p2.getY());
	len = sqrt(x * x + y * y);
}
Line::Line(Line& l) : p1(l.p1), p2(l.p2) {  //组合类的复制构造函数
	cout << "Calling the copy constructor of Line" << endl;
	len = l.len;
}

int main() {
     //下面三种写法完全等效
	cout<<Line ((Point)1,(Point)2).getlen(); 
    cout<<Line(static_cast<Point>(1),static_cast<Point>(2)).getlen();
    cout<<Line(1,2).getlen(); //这个是隐式类型转换,上面两个其实式显式类型转换 
	
}

但是如果我不想用户万一误操作调用隐式构造怎么办?

在构造函数前加上explicit关键字修饰,这样就只能显示转换,使用隐式转换就会报错。

explicit Point(int xx = 0, int yy = 0) {
        x = xx;
        y = yy;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ad_m1n

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

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

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

打赏作者

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

抵扣说明:

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

余额充值