# 为什么调用构造函数失败了?

0
[已解决问题] 解决于 2011-12-30 17:58
`#include <iostream>using namespace std;class Complex{private:    int x;    int y;public:    Complex();    Complex(int x,int y);    Complex(const Complex& complex);    ~Complex();    void display() const;};Complex::Complex(){    cout<<"Complex()"<<endl;}Complex::Complex(int x,int y){    cout<<"Complex(int x,int y)"<<endl;    this->x=x;    this->y=y;}Complex::Complex(const Complex& complex){    this->x=complex.x;    this->y=complex.y;    //Complex(complex.x,complex.y);以上两句为什么不能用左边的那个代替   //就是调用构造函数Complex(int x,int y)    cout<<"Complex(int x,int y)"<<endl;}Complex::~Complex(){    cout<<"~Complex()"<<endl;}void Complex::display() const{    cout<<"x="<<x<<endl<<"y="<<y<<endl;}int main(){    Complex c=Complex(1,2);    c.display();    Complex c2(c);    c2.display();    return 0;}`

`Complex::Complex(const Complex& complex){    this->x=complex.x;    this->y=complex.y;    //Complex(complex.x,complex.y);以上两句为什么不能用左边的那个代替   //就是调用构造函数Complex(int x,int y)    cout<<"Complex(int x,int y)"<<endl;}`
`要是我在复制构造函数里面调用`
`Complex(complex.x,complex.y);那个x,y的值没有赋值成功.`
`为什么呢?`

1

0

Rollen Holt | 园豆：210 (菜鸟二级) | 2012-03-21 16:20

您需要登录以后才能回答，未注册用户请先注册