Skip to content

Instantly share code, notes, and snippets.

@frankie-yanfeng
Created March 3, 2019 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankie-yanfeng/a4028ec7286450e11f2f59436ed3e94f to your computer and use it in GitHub Desktop.
Save frankie-yanfeng/a4028ec7286450e11f2f59436ed3e94f to your computer and use it in GitHub Desktop.
C++ type change constructor
#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