Task 2 Register of Three
Кода минава тестовете но преминава memory лимита. Няма memory leaks мисля предполагам че проблема ми е в assignment оператора но не мога да намеря друг начин да го направя.
#include "Register.h"
Register::Register(size_t numCompanies)
{
numAdded = numCompanies;
companiesArray = new Company[numCompanies];
}
void Register::add(const Company& c)
{
static int index = 0;
companiesArray[index] = c;
++index;
if (index == numAdded)
{
for (size_t i = 0; i < numAdded; i++)
{
--index;
}
}
}
Company Register::get(int companyId) const
{
for (size_t i = 0; i < numAdded; i++)
{
if (companiesArray[i].getId() == companyId)
{
return companiesArray[i];
break;
}
}
}
Register& Register::operator=(const Register& other)
{
if (this != &other)
{
this->numAdded = other.numAdded;
this->companiesArray = new Company[this->numAdded];
*this->companiesArray = *other.companiesArray;
}
return *this;
}
Register::Register(const Register& other)
:numAdded(other.numAdded),companiesArray(other.companiesArray)
{
}
Register::~Register()
{
delete[] companiesArray;
companiesArray = nullptr;
}