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;
}