1.标准数据类型之间会进行隐式的类型安全转换

转换规则如下Slide2.PNG

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. short s = 'a';
  7. unsigned int ui = 1000;
  8. int i = -2000;
  9. double d = i;
  10. cout << "d = " << d << endl;
  11. cout << "ui = " << ui << endl;
  12. cout << "ui + i = " << ui + i << endl; //i隐式转换为 uint 型
  13. if( (ui + i) > 0 )
  14. {
  15. cout << "Positive" << endl;
  16. }
  17. else
  18. {
  19. cout << "Negative" << endl;
  20. }
  21. cout << "sizeof(s + 'b') = " << sizeof(s + 'b') << endl; //short和char默认转换为int类型
  22. return 0;
  23. }
  24. delphi@delphi-vm:~$ ./a.out
  25. d = -2000
  26. ui = 1000
  27. ui + i = 4294966296
  28. Positive
  29. sizeof(s + 'b') = 4
  30. 转为int型进行计算是最高效的,所以把short和char 都转为int型
  31. 小类型初始化大类型是没问题的

隐式类型转换很可能引起bug

2.普通类型与类类型之间能否进行类型转换?类类型之间能否进行类型转换?

3.再论构造函数

—-构造函数可以定义不同类型的参数
—-参数满足下列条件时称为转换构造函数
有且仅有一个参数
参数是基本类型
参数是其它类类型

另一个视角:
旧式的c方式强制类型转换
int i;
Test t;

i = int(1.5);
t = Test(100);

编译器的行为
编译器会尽力尝试让源码通过编译
隐式类型转换
—-会让程序以意想不到的方式进行工作
—-是工程中bug的重要来源

Test t;
t = 100;
Slide8.PNG

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Test
  5. {
  6. int mvalue;
  7. public:
  8. Test()
  9. {
  10. }
  11. Test(int i)
  12. {
  13. cout << "Test(int i)" << endl;
  14. }
  15. Test operator + (const Test& p)
  16. {
  17. Test ret(mvalue + p.mvalue);
  18. return ret;
  19. }
  20. int value()
  21. {
  22. return mvalue;
  23. }
  24. };
  25. int main()
  26. {
  27. Test t;
  28. //t = Test(5); //临时对象赋值给t对象
  29. t = 5; //调用了Test(int i) 是一种隐式类型转换
  30. Test r;
  31. r = t + 10; //这里是误会,但是编译通过了
  32. cout << r.value() << endl;
  33. return 0;
  34. }

4.工程中通过explicit关键字杜绝编译器的转换尝试

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Test
  5. {
  6. int mvalue;
  7. public:
  8. Test()
  9. {
  10. }
  11. explicit Test(int i)
  12. {
  13. cout << "Test(int i)" << endl;
  14. }
  15. Test operator + (const Test& p)
  16. {
  17. Test ret(mvalue + p.mvalue);
  18. return ret;
  19. }
  20. int value()
  21. {
  22. return mvalue;
  23. }
  24. };
  25. int main()
  26. {
  27. Test t;
  28. //t = Test(5); //临时对象赋值给t对象
  29. //t = 5; //error
  30. t = (Test)5; //ok
  31. Test r;
  32. r = t + (Test)10; //ok
  33. cout << r.value() << endl;
  34. return 0;
  35. }

转换构造函数被explicit修饰时只能进行显示转换
—-转换方式
static_cast(Value) //c++推荐的
ClassName(Value); //c语言的
(ClassName)value;//c++不推荐

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Test
  5. {
  6. int mvalue;
  7. public:
  8. Test()
  9. {
  10. mvalue = 0;
  11. }
  12. explicit Test(int i)
  13. {
  14. mvalue = i;
  15. cout << "Test(int i)" << endl;
  16. }
  17. Test operator + (const Test& p)
  18. {
  19. Test ret(mvalue + p.mvalue);
  20. return ret;
  21. }
  22. int value()
  23. {
  24. return mvalue;
  25. }
  26. };
  27. int main()
  28. {
  29. Test t;
  30. //t = Test(5); //临时对象赋值给t对象
  31. //t = 5; //error
  32. t = static_cast<Test>(5); //ok
  33. Test r;
  34. r = t + static_cast<Test>(10); //ok
  35. cout << r.value() << endl;
  36. return 0;
  37. }

小结

1.转换构造函数只有一个参数
2.转换构造函数的参数类型时其它类型
3.转换构造函数在类型转换时被调用
4.隐式类型转换是工程中bug的重要来源
5.explicit关键字用于杜绝隐式类型转换

所以,杜绝隐式类型转换!!!