C++ 类型转换



c++类型转换:将一种类型的值转换为另一种类型的值

类类型:class、stuct、union类型

标准类型:除类类型外的所有类型,如int

类型种类:

类型转换有4种:

1)标准类型->标准类型

2)标准类型->类类型

3)类类型->标准类型

4)类类型->类类型

一、标准类型转变为标准类型

方法:

1)隐式转换

使用场合:程序未显式类型转换,但是含有混合运算,赋值语句,函数返回值,形参和实参转换

2)显式转换

使用场合:程序显式类型转换

方法:C语言的强制法(int)a和C++的函数法int(a)

注意:当类型名有两个以上时(unsign int ),不能使用C++的函数法,但是可以使用C语言的强制法,所以以后可以直接使用C的强制法,不会错

二、标准类型转化成类类型

方法:构造函数和自定义的重载赋值号=的函数

必备参数:它们都需要有标准类型的参数(这个标准类型就是等号右边的值类型)

代码:

  1. #include <iostream>
  2. using namespace std;
  3. class String
  4. {
  5. private:
  6.     int len;
  7.     char* strContent;
  8. public:
  9.     String(int i);
  10.     String(char* p);
  11.     String& operator=(int i);
  12.     String& operator=(char* p);
  13.     void show();
  14. };
  15. String::String(int i)
  16. {
  17.     len=i;
  18.     strContent = new char[len];
  19.     memcpy(strContent,”",len);
  20. }
  21. String::String(char* p)
  22. {
  23.     len=strlen(p)+1;
  24.     strContent = new char[len];
  25.     memcpy(strContent,p,len);
  26. }
  27. String& String::operator=(int i)
  28. {
  29.     len=i;
  30.     if (strContent!=NULL)
  31.     {
  32.         delete []strContent;
  33.     }
  34.     strContent = new char[len];
  35.     memcpy(strContent,”",len);
  36.     return *this;
  37. }
  38. String& String::operator=(char* p)
  39. {
  40.     len=strlen(p)+1;
  41.     if (strContent!=NULL)
  42.     {
  43.         delete []strContent;
  44.     }
  45.     strContent = new char[len];
  46.     memcpy(strContent,p,len);
  47.     return *this;
  48. }
  49. void String::show()
  50. {
  51.     cout<<”字符串长度为:”<<len<<endl;
  52.     cout<<”字符串内容为:”<<strContent<<endl;
  53. }
  54. void main()
  55. {
  56.     String a=1;//调用构造函数
  57.     a.show();
  58.     String b=”123″;//调用构造函数
  59.     b.show();
  60.     b=6;     //调用重载运算符=
  61.     b.show();
  62.     b=”456″;//调用重载运算符=
  63.     b.show();
  64.     system(“pause”);
  65. }

注意:为了避免标准类型隐式转换为类类型(OBJ obj=20),可以把构造函数放在私有段,在创建对象的时候就不能直接调用构造函数了,

这时创建对象可以通过定义一个静态成员函数或一个友元函数 来间接调用构造函数来
代码:

  1. #include <iostream>
  2. using namespace std;
  3. class String
  4. {
  5. private:
  6.     int len;
  7.     char* strContent;
  8.     String(char* p);
  9. public:
  10.     static String make(char* p);
  11.     void show();
  12. };
  13. String::String(char* p)
  14. {
  15.     len=strlen(p)+1;
  16.     strContent = new char[len];
  17.     memcpy(strContent,p,len);
  18. }
  19. String String::make(char* p)
  20. {
  21.     String a=p;//创建一个类,并调用构造函数
  22.     return a;
  23. }
  24. void String::show()
  25. {
  26.     cout<<”字符串长度为:”<<len<<endl;
  27.     cout<<”字符串内容为:”<<strContent<<endl;
  28. }
  29. void main()
  30. {
  31.          String a=”123″;//错误
  32.     String a = String::make(“123456″);
  33.     a.show();
  34.     system(“pause”);
  35. }

三、类类型->标准类型 和 类类型->类类型

方法:引入特殊的成员函数—类型转换函数

类型转换函数:在类对象之间提供一种类似显式类型转换的机制。

语法:

[cpp] view plaincopy


  1. Class 源类类名 //在此类中定义
  2. {
  3.     Operator 目的类型()
  4.     {
  5.         Return 目的类型的数据;
  6.     }
  7. }

作用:将对象转换成目的类型的变量,目的类型如果是标准类型也可以是类类型

注意一:类型转换函数没有参数、没有返回类型、但是有返回值(一个type的实例)。

注意二、类型转换函数只能定义为类的成员函数,而不能是友元函数(不允许有参数)

注意三、类型转换函数不可以被超载,因为没有参数

代码:

  1. #include <iostream>
  2. using namespace std;
  3. class point
  4. {
  5. private:
  6.     int x;
  7. public:
  8.     point()
  9.     {
  10.         x=10;
  11.     }
  12.     operator int() //类型转换函数,转换为整型
  13.     {
  14.         return x;
  15.     }
  16. };
  17. void main()
  18. {
  19.     point a;
  20.     int t=a;//隐式调用 类型转换函数
  21.     cout<<t<<endl;
  22.     int z = a.operator int();//显示调用 类型转换函数
  23.     cout<<z<<endl;
  24.     system(“pause”);
  25. }

注意一:一般使用隐式方式,当需要明确指出用哪一个类型转换函数时,才使用显式方式

注意二:注意下面两种转换方式:

  1. int a = obj;//使用类型转换函数进行转换
  2. OBJ obj=a;  //使用构造函数进行转换

注意三、当一个类既有用于转换的构造函数,又拥有类型转换函数

如:

构造函数:A(int a);

类型转换函数:operator int( )

则,obj=obj+n 就有两种解释:类和标准类型可以互相转化,隐式转换有二义性

解释一:可以先把对象转换为数,再两个数相加

解释二:可以先把数转换成类,再相加

解决方法:这是需要显示使用类型转换函数:

方法一:先把数变对象,两个对象再相加

INT obj1=n;Obj=obj+obj1 两个对象相加

方法二:先把对象变数,两个整数再相加

Obj=(int)obj+n;

即:用户类型定义的类型转换函数,只有无二义性的时候,才能使用隐式转换