C++this指针 重载赋值运算符 使用问题

2025-03-11 06:40:19
推荐回答(4个)
回答1:

你的代码写的没有问题,主要是你没有理解c++的拷贝构造函数的原理,存在深拷贝和浅拷贝。

oldday = newday = birthday;这一连串的赋值语句,中间执行是非常的复杂的。

我们一个个分析下,newday=birthday.这句赋值语句首先会产生个临时的对象。birthday会将其值赋值给临时对象。然后临时对象将将赋值给newday..然后临时对释放掉等。之后 的赋值操作也一致。我新增了拷贝构造函数实现深拷贝。如果不加的话,只是浅拷贝。

class Date


{

int mo,da,yr;

char *month;

public:

Date(int m=0, int d=0, int y=0);

Date(const Date&);                //新装拷贝构造函数,实现深拷贝。

~Date();

Date operator = (const Date&);

void display() const;

};


Date::Date(int m, int d, int y)

{

static char *mos[] =

{

"January","February","March","April","May","June",

"July","August","September","October","November","December"

};

mo = m; 

da = d; 

yr = y;

if (m != 0)

{

month = new char[strlen(mos[m-1])+1];

strcpy(month, mos[m-1]);

}

else   

month = 0;

}


Date::Date(const Date& dt) //  //新装拷贝构造函数,实现深拷贝。

{

if (this != &dt)

{

this->mo = dt.mo;

this->da = dt.da;

this->yr = dt.yr;

if (dt.month != NULL)

{

this->month = new char [strlen(dt.month)+1];

memset(this->month,0,strlen(dt.month)+1);

strcpy(this->month, dt.month);

}

else   

month = NULL;

}

}


Date::~Date()

{

delete [] month;

}


void Date::display() const

{

if (month!=0) 

cout<

}


Date Date::operator=(const Date& dt)

{

if (this != &dt)

{

mo = dt.mo;

da = dt.da;

yr = dt.yr;

delete [] month;

if (dt.month != 0)

{

month = new char [strlen(dt.month)+1];

strcpy(month, dt.month);

}

else   

month = 0;

}

return *this;  //this是这指针,*this就是对指针解引用,就是返回指针所指向的对象,而这个指针所指向的对象就是调用这个类成员函数的对象本身。

}


int main(int argc, char* argv[])

{



Date birthday(8,11,1979);

Date oldday,newday;

oldday = newday = birthday;

birthday.display();

oldday.display();

newday.display();

system("pause");


return 0;

}

回答2:

#include 
#include "string.h"
using namespace std;

class Date
{
int mo,da,yr;
char *month;
public:
Date(int m=0, int d=0, int y=0);
~Date();
Date& operator = (const Date&);
void display() const;
};

Date::Date(int m, int d, int y)
{
static char *mos[] =
{
"January","February","March","April","May","June",
"July","August","September","October","November","December"
};

mo = m; 
da = d; 
yr = y;

if (m != 0)
{
month = new char[strlen(mos[m-1])+1];
strcpy(month, mos[m-1]);
}
else   
month = 0;
}   

Date::~Date()
{
delete [] month;
}

void Date::display() const
{
if (month!=0) 
cout<
}

Date& Date::operator=(const Date& dt)   
{
if (this != &dt)
{
mo = dt.mo;
da = dt.da;
yr = dt.yr;

if(month != 0)
delete [] month;
if (dt.month != 0)
{
month = new char [strlen(dt.month)+1];
memset(month,0,strlen(dt.month)+1);
strcpy(month, dt.month);
}
else   
month = 0;
}

return *this;  //this是这指针,*this就是对指针解引用,就是返回指针所指向的对象,而这个指针所指向的对象就是调用这个类成员函数的对象本身。
}

int main()
{
Date birthday(8,11,1979);
Date oldday,newday;

oldday = newday = birthday;
birthday.display();
newday.display();
oldday.display();

system("pause");
return 0;
}

Date operator = (const Date&);

oldday = newday = birthday;

会生成两个临时对象,然后再执行完这个语句后会调用析构函数。相当于临时对象和newday公用一块内存区域,但是随着临时对象的delete[] month

newday的month区域就变为不可知内容了。

回答3:

你好,问题出在你的
Date operator = (const Date&);
应该是
Date& operator = (const Date&);
必须返回引用类型,不然会产生临时对象并在使用后析构导致你的mouth指针变成野指针,导致输出随机乱码。

回答4:

Date operator = (const Date&);

更改为

Date& operator = (const Date&);

实现中也更改返回类型