gpt4 book ai didi

c++ - 基本深拷贝(操作重载)

转载 作者:太空宇宙 更新时间:2023-11-04 15:38:15 25 4
gpt4 key购买 nike

我正在制作一个简单的类 Person,它继承自抽象类 Object。我正处于学习深拷贝的部分,但我似乎无法让这段简单的代码工作:

Person.h

#include <iostream>
#include <cstring>
#ifndef _PERSON_H_
#define _PERSON_H_
using namespace std;


class Object {
protected:
//This pure virtual func. sends appropriate ostream
//to operator<<'s override. Must be implemented in each
//derived class accordingly.
virtual void print(std::ostream&) const =0;
public:
//Override ostream operator.
friend ostream& operator<<(ostream& os, const Object& obj);
friend ostream& operator<<(ostream& os, const Object* obj);
virtual ~Object();
};

class Person : public Object {
private:
char* m_name;
virtual void print(std::ostream& os) const;
Person(); //What's your default name?
public:
Person(char* name);
Person(const Person& p);
Person& operator=(const Person &rhs);
virtual ~Person();
};

#endif // _PERSON_H_

Person.cpp

#include "Person.h"


ostream& operator<<(ostream& os, const Object& obj) {
obj.print(os);
return os;
}

ostream& operator<<(ostream& os, const Object* obj) {
obj->print(os);
return os;
}
Object::~Object() { };

//Standard ctor
Person::Person(char* name) {
m_name = new char[strlen(name)];
strncpy(m_name, name, strlen(name));
}
//Copy ctor
Person::Person(const Person& p) {
m_name = new char[strlen(p.m_name)];
strncpy(m_name, p.m_name, strlen(p.m_name));
}


//dtor
Person::~Person() {
delete [] m_name;
}
//operator= overload
Person& Person::operator=(const Person &rhs) {
if (this == &rhs) return * this; //Self copy check.
delete [] m_name;
m_name = new char[strlen(rhs.m_name)];
strncpy(m_name, rhs.m_name, strlen(rhs.m_name));
return * this;
}

//private
void Person::print(std::ostream& os) const{
os << m_name;
}

int main() {
//Person p1("Anis");
//Person p2("Bassam");
Person * p1 = new Person("Anis");
Person * p2 = new Person("Bassam");
p1 = p2;
delete p1;
delete p2;
return 0;
}

输出:

a.out(4839,0x7fff7420b300) malloc: *** error for object 0x7f914b404ad0: pointer being freed was not allocated

gdb BT:

#0  0x00007fff8d7b2282 in __pthread_kill ()
#1 0x00007fff8b5fd4c3 in pthread_kill ()
#2 0x00007fff8e528b73 in abort ()
#3 0x00007fff83739937 in free ()
#4 0x0000000100000b51 in Person::~Person (this=0x1001048e0) at Person.cc:29
#5 0x0000000100000b85 in Person::~Person (this=0x1001048e0) at Person.cc:28
#6 0x0000000100000ba8 in Person::~Person (this=0x1001048e0) at Person.cc:28
#7 0x0000000100000e46 in main () at Person.cc:54

程序在 delete p2; 时崩溃,根据 gdb。在书中他们就这种情况警告了我们,所以我遵循了他们提供的修复方法,即重载 operator= 并确保有复制构造函数。我错过了什么?提前致谢。

附注我目前只对深层复制问题感兴趣。模板和泛型将在后面的章节中介绍。

最佳答案

p1=p2之后,两个变量都指向同一个对象。 delete p1 删除这个对象。所以 delete p2 再次尝试删除这个对象。

Operator= 在这里帮不了您,因为您分配的是指针,而不是对象。可能您需要 *p1 = *p2

关于c++ - 基本深拷贝(操作重载),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28246601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com