1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 using namespace std; 4 5 class myclass 6 { 7 public: 8 int x; 9 int y; 10 11 myclass() 12 { 13 14 } 15 //赋值初始化 16 //加上explicit表示不能调用隐式的转换 17 explicit myclass(int a) :x(a), y(a) 18 { 19 cout << "构造" << endl; 20 } 21 22 //赋值重载 23 void operator =(int num) 24 { 25 cout << "赋值重载"; 26 this->x = 3; 27 } 28 29 //类类型转换为int 不能有参数 30 //类类型转换只能是成员函数,必须有this指针 31 operator int() 32 { 33 return (x + y); 34 } 35 36 operator double(); 37 }; 38 39 myclass::operator double() 40 { 41 return x + y; 42 } 43 44 //CPP编译的是从上到下,会遇到不匹配问题 45 //类的声明可以拓展作用域,但是只能创建指针或者引用,不能创建对象 46 //完成类类型转换的时候,一般两个类的转换,两个转换接口都放在一个类 47 48 class mypoint2 49 { 50 public: 51 int a; 52 int b; 53 54 public: 55 mypoint2(int x, int y) :a(x), b(y) 56 { 57 58 } 59 60 mypoint2() :a(0), b(0) 61 { 62 63 } 64 }; 65 66 //该类提供了两个接口,把mypoint转换为mypoint2 mypoint2转换为mypoint1 67 class mypoint 68 { 69 public: 70 int x; 71 int y; 72 int z; 73 mypoint() :x(10), y(20), z(20) 74 { 75 76 } 77 78 //mypoint2类型转换为mypoint类型 79 mypoint(const mypoint2 &my):x(my.a),y(my.b),z(0) 80 { 81 82 } 83 84 //mypoint类型转化为mypoint2类型 85 operator mypoint2() 86 { 87 mypoint2 temp(x, y); 88 return temp; 89 } 90 91 //重载等于号 92 void operator=(const mypoint2 &my) 93 { 94 x = my.a; 95 y = my.b; 96 z = 0; 97 } 98 }; 99 100 void main() 101 { 102 //c++的转换 103 ////自动转换 104 //int a = 1.2; 105 ////显式转换 106 //int b = static_cast<int>(2.3); 107 ////C风格 108 //int c = (int)2.5; 109 110 //int *p1 = nullptr; 111 ////CPP风格的指针转换 112 //char *p2 = reinterpret_cast<char *>(p1); 113 114 //构造函数初始化,视作类型转换 115 /*myclass my1 = static_cast<myclass>(5); 116 myclass my2(2); 117 myclass my3 = myclass(5); 118 myclass my4 = (myclass)5; 119 myclass my5; 120 my5 = 3;*/ 121 122 //调用类类型转换为int类型 123 //myclass my1; 124 //my1.x = 4; 125 //my1.y = 3; 126 //int a = my1; 127 //cout << a << endl; 128 129 mypoint2 my1(1, 2); 130 mypoint my2(my1); 131 //mypoint my3 = my1; 132 133 //调用operator mypoint2() 进行类型转换 134 mypoint2 my4= my2; 135 136 cin.get(); 137 }
类类型转换在实际开发中的用途
1 #include "mainwindow.h" 2 #include <QApplication> 3 #include <QDebug> 4 5 class winsize 6 { 7 public: 8 int x; 9 int y; 10 }; 11 12 class mywin 13 { 14 public: 15 MainWindow *p; 16 int cx; 17 int cy; 18 19 public: 20 mywin():cx(0),cy(0),p(nullptr) 21 { 22 23 } 24 25 //mywin到winsize的类型转换 26 mywin(const winsize &mysize):cx(mysize.x),cy(mysize.y) 27 { 28 p=new MainWindow; 29 p->resize(cx,cy); 30 p->show();; 31 } 32 33 //winsize到mywin的类型转换 34 operator winsize() 35 { 36 winsize tmp{cx,cy}; 37 return tmp; 38 } 39 40 void operator = (const winsize &mysize) 41 { 42 if(p!=nullptr) 43 { 44 p->resize(cx,cy); 45 p->show; 46 } 47 else 48 { 49 p = new MainWindow; 50 p->resize(cx,cy); 51 p->show(); 52 } 53 } 54 55 }; 56 57 int main(int argc, char *argv[]) 58 { 59 QApplication a(argc, argv); 60 winsize win1{200,600}; 61 mywin my = win1; 62 63 //winsize win2 = my; 64 //上述语句的本质 65 winsize win2 = my.operator winsize(); 66 67 qDebug()<<win2.x << win2.y; 68 return a.exec(); 69 }
原文地址:https://www.cnblogs.com/xiaochi/p/8600733.html
时间: 2024-09-29 16:12:44