Created
March 3, 2019 07:25
-
-
Save frankie-yanfeng/a4028ec7286450e11f2f59436ed3e94f to your computer and use it in GitHub Desktop.
C++ type change constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Complex { | |
public: | |
double real, imag; | |
Complex( int i ) { //类型转换构造函数 | |
cout << "IntConstructor called" << endl; | |
real = i; imag = 0; | |
} | |
Complex( double r, double i ) | |
{ real = r; imag = i; } | |
}; | |
int main () { | |
Complex c1(7, 8); | |
Complex c2 = 12; | |
c1 = 9; // 9被自动转换成一个临时Complex对象 | |
cout << c1.real << "," << c1.imag << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment