| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 536 人关注过本帖
标题:关于构造函数的调用
取消只看楼主 加入收藏
gold615
Rank: 2
等 级:论坛游民
帖 子:54
专家分:75
注 册:2014-6-7
结帖率:55.56%
收藏
已结贴  问题点数:20 回复次数:2 
关于构造函数的调用
//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;
}
搜索更多相关主题的帖子: public include Complex called return 
2015-10-04 09:55
gold615
Rank: 2
等 级:论坛游民
帖 子:54
专家分:75
注 册:2014-6-7
收藏
得分:0 
回复 2楼 hjx1120
我觉得这样理解,可以解释结果,但是这个过程还是不明白。它不应该是在return的时候,开辟了一个临时空间,放置由两个int值产生的对象,然后再用这个临时值初始化主函数中的对象?
2015-10-04 12:30
gold615
Rank: 2
等 级:论坛游民
帖 子:54
专家分:75
注 册:2014-6-7
收藏
得分:0 
//稍微更改了一下又试了一下更不懂了,我能理解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;
}
2015-10-04 13:11
快速回复:关于构造函数的调用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012224 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved