简体   繁体   中英

Assignment operator creates pointer and doesn't make deleting possible

As a part of a school assignment, we have to crate an abstract class and use a register class to contain them. The abstract class has two under classes. Like Animal > Dog/Cat

In this task we have to make an assignment operator but after using the one I made, the program gets an issue.

I make two registers

r1; r2;

then use the operator

r2 = r1;

and when the program exits, it goes to the destruktor of r1, removes it, gets to r2 and gets an "Access violation reading location"

I'm guessing this is because the operator creates a pointer from r2 to r1, so when r1 has been deleted.

Operator:

Register& Register::operator=(Registerconst &other)
{
    for (int i = 0; i < this->count; i++)
    {
        delete this->animals[i];
    }
    delete[] this->animals;
    this->name = other.name;
    this->size = other.size;
    this->count = other.count;
    this->animals= new Animal*[this->size];
    for (int i = 0; i < this->count; i++)
    {
        animals[i] = other.animals[i];
    }
    for (int i = count; i < this->size; i++)
    {
        this->animals[i] = nullptr;
    }
    return *this;
}

The destructors aren't virtual. not sure if that's causing it

Due to request here's the place where it's being used

AnimalRegister r1("Name 1");
AnimalRegister r2("Name 2");
// some stuff being added to r1
r2 = r1;
return 0;

the constructor:

AnimalRegister::AnimalRegister(string name)
{
    this->name = name;
    this->size = 10;
    this->count = 0;
    this->animals = new Animal*[this->size];
    for (int i = 0; i < this->size; i++)
        animals[i] = nullptr;
}

The destructor:

AnimalRegister::~AnimalRegister()
{
    for (int i = 0; i < this->count; i++)
        delete animals[i];
    delete[] animals;
}

The reason you are getting an access violation is that you are trying to delete the Animal pointers twice. Your Register assignment operator copies the Animal pointers from source object to the destination object. The destructor of the Register class deletes the Animal pointers it holds -- since these are the same Animal pointers in r1 and r2 you get an access violation when you try and delete them the second time.

You need to decide who owns the Animal pointers. Your question doesn't give me enough information to determine that.

If something outside the Register classes owns the Animals, the Register class should not be deleting them.

If the register class does own them, you need to make deep copies of the animals through some sort of virtual clone() method on Animal.

If the ownership is shared, you should be using std::shared_ptr to hold the Animals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM