一.转换构造函数

将其它类型转换为当前类类型需要借助转换构造函数(Conversion constructor)。转换构造函数也是一种构造函数,它遵循构造函数的一般规则。转换构造函数只有一个参数。

#include <iostream>
using namespace std; //复数类
class Complex{
public:
Complex(): m_real(0.0), m_imag(0.0){ }
Complex(double real, double imag): m_real(real), m_imag(imag){ }
Complex(double real): m_real(real), m_imag(0.0){ } //转换构造函数
public:
friend ostream & operator<<(ostream &out, Complex &c); //友元函数
private:
double m_real; //实部
double m_imag; //虚部
}; //重载>>运算符
ostream & operator<<(ostream &out, Complex &c){
out << c.m_real <<" + "<< c.m_imag <<"i";;
return out;
} int main(){
Complex a(10.0, 20.0);
cout<<a<<endl;
a = 25.5; //调用转换构造函数
cout<<a<<endl;
return ;
}

运行结果:

10 + 20i
25.5 + 0i

二.类型转换函数

#include <iostream>
using namespace std; //复数类
class Complex{
public:
Complex(): m_real(0.0), m_imag(0.0){ }
Complex(double real, double imag): m_real(real), m_imag(imag){ }
public:
friend ostream & operator<<(ostream &out, Complex &c);
friend Complex operator+(const Complex &c1, const Complex &c2);
operator double() const { return m_real; } //类型转换函数
private:
double m_real; //实部
double m_imag; //虚部
}; //重载>>运算符
ostream & operator<<(ostream &out, Complex &c){
out << c.m_real <<" + "<< c.m_imag <<"i";;
return out;
}
//重载+运算符
Complex operator+(const Complex &c1, const Complex &c2){
Complex c;
c.m_real = c1.m_real + c2.m_real;
c.m_imag = c1.m_imag + c2.m_imag;
return c;
} int main(){
Complex c1(24.6, );
double f = c1; //相当于 double f = Complex::operator double(&c1);
cout<<"f = "<<f<<endl; f = 12.5 + c1 + ; //相当于 f = 12.5 + Complex::operator double(&c1) + 6;
cout<<"f = "<<f<<endl; int n = Complex(43.2, 9.3); //先转换为 double,再转换为 int
cout<<"n = "<<n<<endl; return ;
}

运行结果:

f = 24.6
f = 43.1
n = 43

注意:最好不要同时使用转换构造函数和类型转换函数,因为这样会导致语义的二义性。

C++语言基础(22)-转换构造函数和类型转换函数的更多相关文章

  1. [一道搜狗输入法的面试题]C++转换构造函数和类型转换函数

    今天面试遇到一道有关C++转换构造函数的题目,之前经常见到默认构造函数.拷贝构造函数.析构函数,但是从没听说过转换构造函数,隐式转换函数也是一样,C++的确是够博大精深的,学习之路很长啊! 其实我们已 ...

  2. 转载: C++ 转换构造函数 和 类型转换函数

    1.对于系统的预定义基本类型数据,C++提供了两种类型转换方式:隐式类型转换和显式类型转换. ,sum; double b=5.55; sum=a+b;//-------(1) std::cout&l ...

  3. c++转换构造函数和类型转换函数

    看stl源码时,有一段代码感觉很奇怪 iterator begin() { return (link_type)((*node).next); } iterator和link_type是两种不同类型, ...

  4. C++转换构造函数与类型转换构造函数

    转换构造函数: 转换构造函数的只有一个形参: Student(float s) { score = s; age = ; } 如果已经在上面定义了构造函数: Student(); //建立对象c1,由 ...

  5. ndk学习之c++语言基础复习----C++容器、类型转换、异常与文件流操作

    继续来复习C++,比较枯燥,但是这是扎实掌握NDK开发的必经之路,不容小觑. 容器: 容器,就是用来存放东西的盒子. 常用的数据结构包括:数组array, 链表list, 树tree, 栈stack, ...

  6. Java入门 - 语言基础 - 22.异常处理

    原文地址:http://www.work100.net/training/java-exception.html 更多教程:光束云 - 免费课程 异常处理 序号 文内章节 视频 1 概述 2 Exce ...

  7. python自动化--语言基础1--数据类型及类型转换

    Python中核心的数据类型有哪些?变量(数字.字符串.元组.列表.字典) 什么是数据的不可变性?哪些数据类型具有不可变性数据的不可变是指数据不可更改,比如: a = ("abc" ...

  8. C++语言基础(23)-拷贝构造函数

    当以拷贝的方式初始化一个对象时,会调用一个特殊的构造函数,就是拷贝构造函数(Copy Constructor). 例如: #include <iostream> #include < ...

  9. C++语言基础(13)-抽象类和纯虚函数

    一.基本语法 在C++中,可以将虚函数声明为纯虚函数,语法格式为: ; 纯虚函数没有函数体,只有函数声明,在虚函数声明的结尾加上=0,表明此函数为纯虚函数. 最后的=0并不表示函数返回值为0,它只起形 ...

随机推荐

  1. 回顾苹果操作系统Mac OS的发展历史

    在新的MacBook AIR和Mac OS X Lion即将发布之际,我们仅以此文向伟大的苹果和乔布斯致敬.并祝Apple教主乔布斯早日康复,长命百岁,千秋万载,一统苹果! Mac OS是指运行于苹果 ...

  2. selenium+python自动化88-批量操作循环点击报错:Element not found in the cache - perhaps the page has changed since it was looked up

    前言 selenium定位一组元素,批量操作循环点击的时候会报错:Element not found in the cache - perhaps the page has changed since ...

  3. 报错:this class is not key value coding-compliant for the key closeLotTextField解决方法

    几种情况下都会报这种错误: 1,加载自定义的tableViewCell的时候总是死在: XInstrumentOpenCell *cell = [tableViewdequeueReusableCel ...

  4. Cannot generate SSPI context

    请在参考如下文章前,重启服务器解决! 相信我 没错的! http://support.microsoft.com/kb/811889

  5. 非意外的PDB错误 OK(0)

    用ib编项目会出现这个error 用vs重新编译全部 就没有问题 ib的设置改下 Visual Studio Builds--Advanced --PDB File Allocation Force ...

  6. Eight_pku_1077(广搜).java

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21718   Accepted: 9611   Special ...

  7. [Functional Programming Monad] Combine Stateful Computations Using Composition

    We explore a means to represent the combination of our stateful computations using familiar composit ...

  8. C++ Primer笔记2_四种类型转换_异常机制

    1.类型转换 命名的强制类型转换: 有static_cast.dynamic_cast.const_cast.reinterpret_cast static_cast: 编译器隐式运行的不论什么类型转 ...

  9. Solidworks如何添加齿轮

    打开ToolBox,找到GB,动力传动,齿轮,正齿轮,然后拖放到绘图窗口(切记要在装配图里面弄,不是在单个零件里面弄)   设置齿轮的参数,一般只需要设置模数,齿数,面宽,类型,总长度(面宽就是有齿轮 ...

  10. Cocos2d-x中如何增加图片和文本菜单

    菜单都以MenuItem开头 MenuItemLabel - 文本菜单项 MenuItemImage - 图片菜单项 // on "init" you need to initia ...