64,637
社区成员
发帖
与我相关
我的任务
分享
#include "Date.h"
class Person
{
public:
Person(int id, int year, int month, int day);
Person(Person &); // 问题出在这个方法里
~Person();
int getId();
Date* getBirthDate(); // return the pointer of the object
private:
int id;
Date* birthDate; // The pointer of the object
};
#ifndef DATE_H
#define DATE_H
class Date{
public:
Date(int newYear, int newMonth, int newDay);
int getYear();
void setYear(int year);
private:
int year;
int month;
int day;
};
#endif
Person::Person(Person &person){
id = person.id;
Date *p = person.getBirthDate();
birthDate = new Date(*p); // 调试的时候一切正常
}
Person person1(111, 1970, 5, 3);
Person person2(222, 2000, 11, 8);
person1 = Person(person2); // Copy person2 to person1
#include <cstdio>
#ifndef DATE_H
#define DATE_H
class Date{
public:
Date(int newYear, int newMonth, int newDay) : year(newYear), month(newMonth), day(newDay){}
int getYear(){return year;}
void setYear(int newYear){year=newYear;}
private:
int year;
int month;
int day;
};
#endif
//#include "Date.h"
class Person
{
public:
Person(int id, int year, int month, int day) : id(id), birthDate(new Date(year, month, day)){}
Person(Person &person){
id = person.id;
Date *p = person.getBirthDate();
birthDate = new Date(*p); // 调试的时候一切正常
}
~Person(){delete birthDate;}
int getId(){return id;}
Date* getBirthDate(){return birthDate;}; // return the pointer of the object
Person& operator = (const Person& other)
{
Date* temp = new Date(*other.birthDate);
delete birthDate;
birthDate = temp;
id = other.id;
return *this;
}
private:
int id;
Date* birthDate; // The pointer of the object
};
int main()
{
Person person1(111, 1970, 5, 3);
Person person2(222, 2000, 11, 8);
person1 = Person(person2); // Copy person2 to person1
printf("%d\n", person1.getBirthDate()->getYear());
return 0;
}
楼主33楼的问题在于
Date* getBirthDate()
不是常函数(后面要长const小尾巴的才是常函数)
而Person& Person::operator=(const Person& person)
中形参person是一个常引用,所以
birthDate = new Date(*(person.getBirthDate()));
不能编译过,因为不能直接调用常函数。
在这里,不需要通过getBirthDate来访问birthDate,如我10楼代码,没有用getBirthDate。
另一方面,代码没有判断this和另一个操作数是不是同一个,每次先保证new一个birthDate出来,再
释放已有的,然后拷贝id,指针赋值,保证这个时候不出错。Person::Person(Person &person){
this->operator = person;
}
Person& Person::operator=(const Person& person){
if(birthDate)
delete birthDate;//若对象已有birthData,需要析构再创建,避免出现内存泄露
id = person.id;
birthDate = new Date(*person.getBirthDate());
return *this;
}
错误是不能将this指针从const Person转换为Person &就在birthDate = new Date(*person.getBirthDate());这一行struct Date{
public:
Date(int newYear, int newMonth, int newDay):year(newYear),month(newMonth),day(newDay){};
Date(const Date &ref ):year(ref.year),month(ref.month),day(ref.day){};
Date operator=(const Date &ref){
year =ref.year;month = ref.month; day =ref.day;
return *this;
};
int year;
int month;
int day;
};
class Person
{
public:
Person(int id, int year, int month, int day);
Person(int id, const Date & ref);
Person(const Person &);
int getId()const{return id;};
const Date getBirthDate()const{return birthDate;};
Person& Person::operator=(const Person& person);
private:
int id;
Date birthDate; // The pointer of the object
};
Person::Person(int id, int year, int month, int day):id(id),birthDate(year,month,day){}
Person(int id, const Date & ref):id(ref.id),birthDate(ref.birthDate){}
Person::Person(const Person & ref):id(ref.id),birthDate(ref){}
Person& Person::operator=(const Person& person){
if(this ==&person)return *this;
id = person.id;
birthDate = person.birthDate ;
return *this;
}
Person& Person::operator=(const Person& person){
if(birthDate)
delete birthDate;//若对象已有birthData,需要析构再创建,避免出现内存泄露
id = person.id;
Date *p = person.getBirthDate();//编译的时候显示不能将this指针从const Person转化为Person&
birthDate = new Date(*p);
return *this;
}
[/quote]
恩,是有个问题,直接birthDate = new Date(*person.getBirthDate());
Person& Person::operator=(const Person& person){
if(birthDate)
delete birthDate;//若对象已有birthData,需要析构再创建,避免出现内存泄露
id = person.id;
Date *p = person.getBirthDate();//编译的时候显示不能将this指针从const Person转化为Person&
birthDate = new Date(*p);
return *this;
}