/*
类型转换函数能够实现把一个类 类型 转换成 基本数据类型(int、float、double、char等) 或者 另一个类 类型。
其定义形式如下,注意不能有返回值,不能有参数,只能返回要转换的数据类型
*/
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class CDataCollection{
private:
char cVar;
int iVar;
float fVar;
double dVar;
string sVar;
public:
CDataCollection(char c, int i, float f, double d, string s){cVar = c; iVar = i; fVar = f; dVar = d; sVar = s;}
operator char(){return cVar;}
operator int(){return iVar;}
operator float(){return fVar;}
operator double(){return dVar;}
operator string(){return sVar;}
};
void OperatorTst()
{
CDataCollection oDataCollection('i', 1, 3.14, 3.1415926,"Phone");
char cVar = oDataCollection; cout << "CDataCollection--->char : " << cVar << endl;
int iVar = oDataCollection; cout << "CDataCollection--->int : " << iVar << endl;
float fVar = oDataCollection; cout << "CDataCollection--->float : " << fVar << endl;
double dVar = oDataCollection; cout << "CDataCollection--->double : " << dVar << endl;
string sVar = oDataCollection; cout << "CDataCollection--->string : " << sVar << endl;
}
void main()
{
OperatorTst();
}类型转换函数operator Type();
最新推荐文章于 2024-01-05 20:34:52 发布
本文介绍了一种特殊类型的成员函数——类型转换函数,该函数可以将一个类类型转换为基本数据类型或其他类类型。通过示例代码展示了如何定义和使用这些转换函数,并提供了具体的输出结果。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
ACE-Step
音乐合成
ACE-Step
ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言
7739

被折叠的 条评论
为什么被折叠?



