qwdqwefwf 2023-03-02 21:41 采纳率: 100%
浏览 25
已结题

关于C++析构函数的自动调用的问题

请问这个程序中c_1,c_3的析构函数为什么没有调用呀

#include <iostream>

using namespace std;
class CCountry{
    private:
        char *m_name;
        double m_area;
    public:
        CCountry();
        CCountry(char*,double);
    //析构函数
        ~CCountry();

        CCountry(const CCountry &obj);
        void Print();
        int CompareArea(CCountry);
};

CCountry::CCountry()
{
    cout<<"无参构造函数"<<endl;
}

CCountry::CCountry(char* name,double area)
{   //char*不能直接转string
    string str=name;
    cout<<str<<"的含参构造函数"<<endl;
    m_area=area;
    m_name=name;
}

CCountry::~CCountry()
{   
    string str=m_name;
    cout<<str<<"的析构函数"<<endl;
}

CCountry::CCountry(const CCountry &obj)
{
    m_area=obj.m_area;
    m_name=obj.m_name;
    string str=m_name;
    cout<<str<<"的拷贝构造函数"<<endl;
}
void CCountry::Print()
{
    cout<<m_name<<"  "<<m_area<<endl;
}

//该对象比传入对象大,返回1
int CCountry::CompareArea(CCountry c)
{
    if(m_area>c.m_area)
       {return 1;} 
    if(m_area<c.m_area)
        {return 0;}
    if(m_area==c.m_area)
        {return 2;}
    return -1;
}

int main()
{   cout<<"dsqw"<<endl;
    CCountry c_1((char*)"coutry1",20.0);
    CCountry *c_2 =new CCountry((char*)"country2",30);
    CCountry c_3(c_1);
    CCountry c_4((char*)"country4",40);
    c_1.Print();
    c_2 ->Print();
    c_3.Print();
    c_4.Print();
    cout<<c_1.CompareArea(c_4)<<endl;
    delete(c_2);
    system("pause");
}

img

  • 写回答

1条回答 默认 最新

  • 真相重于对错 2023-03-03 10:29
    关注

    调用了,只是你没看到
    你把maiin函数里面的代码放到一个函数里,然后main函数调用那个函数,自然就会看到

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月11日
  • 已采纳回答 3月3日
  • 创建了问题 3月2日

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败