一、转换构造函数的学习:
1、回忆数据类型转换:
在平时写代码的时候,最怕的就是那种隐式数据类型转换了,一不小心,软件就bug不断;而显示数据类型(一般是程序自己去强制类型转换,这个是我们能够明显的识别和掌控的)。为此我们这里总结了一副隐式类型转换的图:
下面我们来几个隐式转换的例子:
代码版本一:
#include <iostream>
#include <string>
int main()
{
short s ='a';
unsigned int ui = 100;
int i = -200;
double d = i;
std::cout<<"d =" << d <<std::endl;
std::cout<<"ui= "<<ui<<std::endl;
if((ui+i)>0)
{
std::cout<<"postive"<<std::endl;
}
else
{
std::cout<<"negative"<<std::endl;
}
return 0;
}
输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
postive
注解:这里我们明显发现(-200+100)还是大于0,这显然不符合正常人的思维了;所以我们仔细分析一下,发现这里肯定是进行了隐式转换了,为此我们再加一条语句看看(ui+i)的值到底是多少:
代码版本二:
#include <iostream>
#include <string>
int main()
{
short s ='a';
unsigned int ui = 100;
int i = -200;
double d = i;
std::cout<<"d =" << d <<std::endl;
std::cout<<"ui= "<<ui<<std::endl;
if((ui+i)>0)
{
std::cout<<"(ui+i) = "<<ui+i<<std::endl;
std::cout<<"postive"<<std::endl;
}
else
{
std::cout<<"negative"<<std::endl;
}
return 0;
}
输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
(ui+i) = 4294967196
postive
注解:通过打印(ui+i)的值我们发现,i原本是int数据类型,这里隐式转换成无符号的数据类型了
为了让大家更加理解隐式的转换,我们下面再来一个例子:
代码版本三:
#include <iostream>
#include <string>
int main()
{
short s ='a';
unsigned int ui = 100;
int i = -200;
double d = i;
std::cout<<"d =" << d <<std::endl;
std::cout<<"ui= "<<ui<<std::endl;
if((ui+i)>0)
{
std::cout<<"(ui+i) = "<<ui+i<<std::endl;
std::cout<<"postive"<<std::endl;
}
else
{
std::cout<<"negative"<<std::endl;
}
std::cout<<"sizeof(s+'b') = "<<sizeof(s+'b')<<std::endl;
return 0;
}
输出结果:
root@txp-virtual-machine:/home/txp# g++ test.cpp
root@txp-virtual-machine:/home/txp# ./a.out
d =-200
ui= 100
(ui+i) = 4294967196
postive
sizeof(s+'b') = 4
注解:这里我们发现sizeof出来的内存大小是4个字节大小;其实这里编译器把short和char类型的都转换int类型了,所以最终两个int数据相加,所占的内存大小就是int类型了。
所以咋们平时在写代码的时候,脑袋里面要有这种写代码谨慎的思维,防止出现这种隐式转换的情况出现,养成写代码的好习惯
2、普通类型与类类型之间能否进行类型转换,类类型之间又是否能够类型转换呢?
为了说明这些问题,咋们通过实际的代码测试来看看啥情况:
代码:普通类型转换成类类型
#include <iostream>
#include <string>
class test{
public:
test()
{
}
test(int i)
{
}
};
int main()
{
test t;
t =6; 从 c 语言角度,这里将 5 强制类型转换到 test 类型,只不过编译器 在这里做了隐式类型转换
return 0;
}
输出结果(显示可以编译通过)
root@txp-virtual-machine:/home/txp# g++ test.cpp
root@txp-virtual-machine:/home/txp# ./a.out
代码类类型转换为普通类型
#include <iostream>
#include <string>
class test{
public:
test()
{
}
test(int i)
{
}
};
int main()
{
test t;
int i = t;
return 0;
}
输出结果(没有编译通过)
root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: in function ‘int main()’:
test.cpp:21:14: error: cannot convert ‘test’ to ‘int’ in initialization
int i = t;
^
代码类类型与类类型之间的转换:
#include <iostream>
#include <string>
class value{
};
class test{
public:
test()
{
}
test(int i)
{
}
};
int main()
{
test t;
value i;
t=i;
return 0;
}
输出结果(暂时还是不行,编译不通过):
root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: in function ‘int main()’:
test.cpp:27:7: error: no match for ‘operator=’ (operand types are ‘test’ and ‘value’)
t=i;
^
test.cpp:27:7: note: candidate is:
test.cpp:9:7: note: test& test::operator=(const test&)
class test{
^
test.cpp:9:7: note: no known conversion for argument 1 from ‘value’ to ‘const test&’
说明:上面的例子,我们只是简单的按照实际角度出发,发现确实有写转换行不通。那么真理到底是怎样的?我们接着往下看
搞嵌入式的为什么一定要学RTOS?
联想小新Air14 2019锐龙版上架 首发优惠价3799元
CC2500RGPR 2.4G射频收发器的产品介绍
三菱飞机公司宣布将SpaceJet支线喷气式客机的交付时间至少推迟一年
光电与半导体领域双管齐下,天电光电助力智慧照明发展
C++之类型转换函数详解
内存时序对内存性能的影响有哪些
新技术 新工艺 BLDC电机如何增效降本
18年电感厂家告诉你磁环电感和线圈电感的区别
为什么有的二极管只能单向导电
如果你想找TI物联网技术资料,请看这里!
SAP ERP公有云产品的六大主要模块
华为Mate20Pro怎么样 手机体验更加自然人性化
华为全新nova 6 5G真机宣传海报亮相,采用“药丸”屏幕设计方案
现代服务器系统上PCIe时序设计的解决方案
直播内容抢先看 | 车辆动力学模型在仿真测试中的应用实践
消息称陈旭东重回联想接管全球PC业务
上海铁塔助力上海小汤山医院成功完成了5G信号开通
提供了更准确、可靠和精密的电阻测量方法:pcb开尔文走线
十四五期间大同电网将走向何处?