在cout之前调用c ++析构函数

问题描述 投票:-2回答:1
#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”被搞砸了,显示出一个随机字符。我认为析构函数在显示之前被调用。如果我删除位置析构函数,它的工作原理。我该如何解决?我很抱歉我的错误,但我是新手。

c++ class destructor
1个回答
2
投票

首先,最好使用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;
}
© www.soinside.com 2019 - 2024. All rights reserved.