注册 登录
编程论坛 C++教室

关于构造函数的调用

gold615 发布于 2015-10-04 09:55, 536 次点击
//c++中构造函数不是没出现一个对象就要调用一次对应的构造函数么?那下面这个程序中,所有的对象加起来一共出现了六次,但为什么显示似乎只调用了五次?
#include<iostream>
#include<cstdlib>
using namespace std;
class Complex
{
    public:
        Complex(int a=0,int b=0)
        {
            cout<<"called\n";
            count++;
            x=a;y=b;
        }
        int Getx()const
        {
            return x;            
        }
        int Gety()const
        {
            return y;            
        }
        void show()const
        {
            cout<<"x="<<x<<"\ty="<<y<<endl;
        }
        friend Complex operator+(Complex ,Complex);
        friend Complex operator-(Complex,Complex);
        friend Complex operator-(Complex);
        static int count;
    private:
        int x;int y;
        
};
int Complex::count=0;
Complex operator+(Complex m,Complex n)
{
    int a,b;
    a=m.Getx()+n.Getx();
    b=m.Gety()+n.Gety();
    return Complex(a,b);
}
Complex operator-(Complex m,Complex n)
{
    int a,b;
    a=m.Getx()-n.Getx();
    b=m.Gety()-n.Gety();
    return Complex(a,b);
}
Complex operator-(Complex m)
{
    int a,b;
    a=-m.Getx();
    b=-m.Gety();
    return Complex(a,b);
}
int main()
{
    Complex a(1,1),b(2,2);//调用两次构造函数
    a.show();
    b.show();
    Complex c=a+b;//此处应该是调用调用两次构造函数
    c.show();
    c=a-b;//此处应该调用一次
    c.show();
    c=-c;//此处应该调用一次
    c.show();
    cout<<c.count<<endl;
}
7 回复
#2
hjx11202015-10-04 10:37
Complex operator+(Complex m,Complex n)
{
    int a,b;
    a=m.Getx()+n.Getx();
    b=m.Gety()+n.Gety();
    return Complex(a,b);
}

这个共有函数体算是一次吧
#3
gold6152015-10-04 12:30
回复 2楼 hjx1120
我觉得这样理解,可以解释结果,但是这个过程还是不明白。它不应该是在return的时候,开辟了一个临时空间,放置由两个int值产生的对象,然后再用这个临时值初始化主函数中的对象?
#4
gold6152015-10-04 13:11
//稍微更改了一下又试了一下更不懂了,我能理解Complex c=a+b这一步确实只调用一次自己写的构造函数,而c以及子程序中m与n均是默认的不带参数的构造函数
//但是为什么这一步析构的时候只析构掉两个,而再下一步c=a-b的时候析构却把子函数中的m、n、return临时返回值均析构了。求解答啊
#include<iostream>
#include<cstdlib>
using namespace std;
class Complex
{
    public:
        ~Complex()
        {
            cout<<"析构函数调用\n";
        }
        Complex(int a=0,int b=0)
        {
            cout<<"构造函数调用\n";
            count++;
            x=a;y=b;
        }
        int Getx()const
        {
            return x;            
        }
        int Gety()const
        {
            return y;            
        }
        void show()const
        {
            cout<<"x="<<x<<"\ty="<<y<<endl;
        }
        friend Complex operator+(Complex ,Complex);
        friend Complex operator-(Complex,Complex);
        friend Complex operator-(Complex);
        static int count;
    private:
        int x;int y;
        
};
int Complex::count=0;
Complex operator+(Complex m,Complex n)
{
    int a,b;
    a=m.Getx()+n.Getx();
    b=m.Gety()+n.Gety();
    return Complex(a,b);
}
Complex operator-(Complex m,Complex n)
{
    int a,b;
    a=m.Getx()-n.Getx();
    b=m.Gety()-n.Gety();
    return Complex(a,b);
}
Complex operator-(Complex m)
{
    int a,b;
    a=-m.Getx();
    b=-m.Gety();
    return Complex(a,b);
}
int main()
{
    Complex a(1,1),b(2,2);//调用两次构造函数
    a.show();
    b.show();
    Complex c=a+b;//此处应该是调用调用两次构造函数
    c.show();
    c=a-b;//此处应该调用一次
    c.show();
    c=-c;//此处应该调用一次
    c.show();
    cout<<c.count<<endl;
}
#5
yangfrancis2015-10-04 19:26
complex c=a+b是一次调用吧?
#6
TonyDeng2015-10-04 20:41
Complex c=a+b;//此处应该是调用调用两次构造函数


誰告訴你這裡要調用兩次構造函數?你根本就沒搞清楚構造函數是怎麼回事。連同前面的析構函數,也是同樣的問題。我看你看的書是完全不講原理祗講語法的,抑或是你沒看?
#7
TonyDeng2015-10-04 21:40
程序代码:

#include <iostream>
#include <string>

const std::string Wait_Message("Press any key to continue...");

// 複數類定義
class Complex
{
    public:

        Complex(double x, double y) : _x(x), _y(y)
        {
            std::cout << "Create: (" << _x << ", " << _y << ")" << std::endl;
        }

        ~Complex()
        {
            std::cout << "Destroy:(" << _x << ", " << _y << ")" << std::endl;
        }

        friend Complex operator+(const Complex& a, const Complex& b)
        {
            return Complex(a._x + b._x, a._y + b._y);
        }

    private:
        
        double _x;        // 實部
        double _y;        // 虛部
};

void Pause(const std::string msg)
{
    std::cout << std::endl << msg;
    std::cin.get();
}

void Test(Complex& x)
{
    Complex a(11, 12);
    Complex b(a + x);
}

int main(void)
{
    Complex a(1, 2);
    Complex b(3, 4);
    Test(a + b);

    Pause(Wait_Message);
    return 0;
}


只有本站会员才能查看附件,请 登录
#8
TonyDeng2015-10-04 22:32
1