64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Complex
{
private:
double m_real;
double m_imag;
int id;
static int counter;
public:
// 无参数构造函数
Complex(void)
{
m_real = 0.0;
m_imag = 0.0;
id = (++counter);
cout << "Complex(void):id=" << id << endl;
}
// 复制构造函数(也称为拷贝构造函数)
Complex(const Complex & c)
{
// 将对象c中的数据成员值复制过来
m_real = c.m_real;
m_imag = c.m_imag;
id = (++counter);
cout << "Complex(const Complex&):id=" << id << " from id=" << c.id << endl;
}
// 类型转换构造函数,根据一个指定的类型的对象创建一个本类的对象
~Complex()
{
cout << "~Complex():id=" << id << endl;
}
};
int Complex::counter = 0;
Complex test1(const Complex& c)
{
return c;
}
Complex test2(const Complex c)
{
return c;
}
int main()
{
Complex a, b;
test1(a);
test2(a);
return 0;
}