【问题标题】:c++ destructor called before cout在 cout 之前调用的 c++ 析构函数
【发布时间】:2018-04-24 15:54:21
【问题描述】:
#include <iostream>
#include <string.h>
using namespace std;


class Location
{
double lat, lon;
char *emi;

public:
Location(int =0, int=0, const char* =NULL);
~Location();
Location (const Location&);
void print () const;
friend ostream& operator<< (ostream&, const Location &);
void operator! ();


protected: ;

private:
};

Location::Location(int lat, int lon, const char *emi) 
{
this->lat=lat;
this->lon=lon;
if (emi!=NULL)
{
this->emi=new char [strlen (emi)+1];
strcpy (this->emi, emi);
}

}

Location::~Location()
{
if (emi!=NULL)
delete []emi;

}

Location::Location(const Location &l)
{
lat=l.lat;
lon=l.lon;
if (l.emi!=NULL)
{
emi=new char [strlen (l.emi)+1];
strcpy (emi, l.emi);
}
}
void Location::operator! ()
{
if (!(strcmp(this->emi, "north")))
strcpy (this->emi, "south");
else strcpy (this->emi, "north");
}


void Location::print() const
{
cout<<this->emi<<endl;
cout<<this->lon<<endl;
cout<<this->lat<<endl;
cout<<endl;
}
class Adress
{
char *des;
Location l1;
char *country;
public:
Adress(char *,const Location &, char *);
virtual ~Adress();
friend ostream& operator<< (ostream&, const Adress &);



protected:

private:
};


Adress::Adress(char *des,const  Location &l1, char *country)
{
if (des!=NULL)
{
this->des=new char [strlen (des)+1];
strcpy (this->des, des);
}
if (country!=NULL)
{
this->country=new char [strlen (country)+1];
strcpy (this->country, country);
}
this->l1=l1;
}

Adress::~Adress()
{
if (country!=NULL)
delete []country;
if (des!=NULL)
delete []des;
}
ostream& operator<< (ostream &os, const Adress& a){
os <<"Descrition: " << a.des<<endl;
os<<"Country: "<<a.country<<endl;
a.l1.print();
return os;
}

int main ()
{
Adress a1 ("dsad", Location (323, 34, "fdsf"), "fsdf");
cout<<a1;
}

问题是,当我创建一个 Adress 对象并显示它时,所有字段都是正确的,但是“emi”却搞砸了,显示了一个随机字符。我认为在显示之前调用了析构函数。如果我删除 Location 析构函数,它就可以工作。我应该如何解决?我很抱歉我的错误,但我是新手。

【问题讨论】:

  • 请编辑您的问题以包含minimal reproducible example
  • 请贴出真实代码,即使添加了缺失的部分也无法编译
  • 你没有为Locationoperator=是故意的吗?这将使用简单地复制指针的默认实现。这个指针指向当原始实例离开作用域时被删除的东西。
  • 这显然不是您自己实际编译和测试的代码。请edit修复错误;您还应该包含必要的标题和main() 函数以使其成为minimal reproducible example。附:拼写:地址(两个d)
  • 不相关,但你为什么写课程而不使用std::string

标签: c++ class destructor


【解决方案1】:

首先,使用 std::string 而不是 char* 会更好,但我会为教育目标解释你的问题。

您必须确保在构造对象后,其所有成员变量都已初始化。例如,在 Location 类的情况下;如果构造函数的第三个参数是 nullptr,则您没有初始化 emi 成员变量。所以我改变了一点:

Location::Location(int _lat, int _lon, const char* _emi)
    : lat(_lat)
    , lon(_lon)
    , emi(nullptr)
{
    if (_emi != nullptr)
    {
        emi = new char[strlen(_emi) + 1];
        strcpy(emi, _emi);
    }
}

接下来,您的类中有一个原始指针,您不能简单地复制或分配它。您必须实现赋值运算符以及复制构造函数。

Location& Location::operator=(const Location& other)
{
    if (this != &other)
    {
        lat = other.lat;
        lon = other.lon;
        if (emi) delete[] emi;
        emi = new char[strlen(other.emi) + 1];
        strcpy(emi, other.emi);
    }
    return *this;
}

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 2018-01-02
    • 1970-01-01
    • 2014-05-02
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多