C++关于类的强制类型转换

本文深入解析了C++ Primer Plus 第六版中关于类的强制类型转换的内容,包括内置类型转换为自定义类及自定义类转换为内置类型的方法。通过具体例子展示了如何实现这一功能,并提供了完整的类定义和测试代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在看《C++ Primer Plus》第六版的过程中,第十一章的后面部分讲到了关于类的强制类型转换。其中分为将内置类型转换为自定义类将自定义类转换为内置类型


1、将内置类型转换为自定义类

将内置类型转换为自定义类的方法很简单,即调用在类中定义的只有一个参数的构造函数。比如有一个Stonewt类声明如下(跟如何定义成员函数没有多大关系):

// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
	enum {lbs_per_stn = 14};			// pounds per stone
	int stone;							// whole stones
	double pds_left;					// fractional pounds
	double pounds;						// entire weight in pounds
public:
	Stonewt(double lbs);				// constructor for double pounds
	Stonewt(int stn, double lbs);		// constructor for stone, lbs
	Stonewt();							// default constructor
	~Stonewt();
	void show_lbs() const;				// show weight in pounds format
	void show_stn() const;				// show weigth in stone format
	// conversion functions
	operator int() const;
	operator double() const;
};
#endif
可以用如下语句将某种数值类型转换为Stonewt类型

Stonewt x = 255.5;
Stonewt y = 12;
第一句将直接调用类的只有一个参数的构造函数(如果存在的话);

第二句也会调用只有一个参数的构造函数,但是12会被先转换为double型。


2、将自定义类转换为内置类型

在C++中,使用转换函数来执行强制类型转换,函数声明格式如下:

operator TypeName();
注意几点:

i)转换函数必须是类成员函数

ii)转换函数不能有参数

iii)不能指定转换函数的返回类型

还是用上面的例子,转换函数已经声明在类中了,我们可以如下定义他们,让类的实例可以被强制转换为 int 类型 或 double 类型:

Stonewt::operator int() const
{
	return int (pounds + 0.5);
}

Stonewt::operator double() const
{
	return pounds;
}
这样就可以如下对类使用强制类型转换了:

Stonewt x = 123.4;
int y;
y = int(x);

---------------------

另外,上面的类出自于《C++ Primer Plus》第六版

完整类定义如下:

// stonewt.cpp -- Stonewt methods
#include <iostream>
#include "stonewt.h"
using std::cout;

Stonewt::Stonewt(double lbs)
{
	stone = int(lbs) / lbs_per_stn; 		// integer division
	pds_left = int(lbs) % lbs_per_stn + lbs - int(lbs);
	pounds = lbs;
}

Stonewt::Stonewt(int stn, double lbs)
{
	stone = stn;
	pds_left = lbs;
	pounds = stn * lbs_per_stn + lbs;
}

Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
}

Stonewt::~Stonewt() {}

// show weight in stones
void Stonewt::show_stn() const
{
	cout << stone << " stones, " << pds_left << " pounds\n";
}

// show weight in pounds
void Stonewt::show_lbs() const
{
	cout << pounds << " pounds\n";
}

Stonewt::operator int() const
{
	return int (pounds + 0.5);
}

Stonewt::operator double() const
{
	return pounds;
}

测试代码如下:

// stone.cpp -- user-defined conversions
// compile with stonewt.cpp
#include <iostream>
#include <cstdlib>
#include "stonewt.h"
using std::cout;
void display(const Stonewt & st, int n);

int main()
{
	Stonewt incognito = 275;
	Stonewt wolfe(285.7);
	Stonewt taft(21, 8);

	cout << "The celeberity weight ";
	incognito.show_stn();
	cout << "The detective weight ";
	wolfe.show_stn();
	cout << "The President weight ";
	taft.show_lbs();

	incognito = 276.8;
	taft = 325;
	cout << "After dinner, the celeberity weight ";
	incognito.show_stn();
	cout << "After dinner, the President weight ";
	taft.show_lbs();
	display(taft, 2);
	cout << "The wrestler weight even more.\n";
	display(422, 2);
	cout << "No stone left unearned\n";

	int x;
	Stonewt y = 235.5;
	cout << "before conversion: ";
	y.show_lbs();
	x = int(y);
	cout << "after conversion: " << x << "\n";

	system("pause");
	return 0;
}

void display(const Stonewt & st, int n)
{
	for(int i = 0; i < n; ++i)
	{
		cout << "Wow! ";
		st.show_stn();
	}
}


如有纰漏,请批评指正:artprog@163.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值