I currently have an assignment operator that looks like this:
CellPhoneHandler CellPhoneHandler:: operator=(const CellPhoneHandler &original){
if (this->nrOfCellphones > 0)
for (int i = 0; i < this->nrOfCellphones; i++) {
delete this->cellphone[i];
}
delete[] cellphone;
this->nrOfCellphones = original.nrOfCellphones;
this->cellphone = new CellPhone*[this->nrOfCellphones];
for (int i = 0; i<this->nrOfCellphones; i++)
{
cellphone[i] = original.cellphone[i];
}
return *this;
}
and then in the start of the program, I intend to test that it is working:
CellPhoneHandler assignme;
assignme.addPhone("assignment phone", 500, 1000);
assignme.addPhone("assignment phone 2", 500, 1000);
copyme = assignme;
Hower, when i exit the program i get and unhandled exception which points to line 52 in dbgdel.cpp:
/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
Any ideas on why? The problem seems to lie only within this functions, as it works when i comment the test away from the program.
my CellPhone class looks like this:
class CellPhone
{
private:
string model;
int stock;
double price;
public:
CellPhone(string model="", int stock=0, double price=0); // constructor
string getModel()const;
int getStock()const;
double getPrice()const;
void setModel(string model);
void setStock(int stock);
void setPrice(double price);
string toString() const;
~CellPhone(); //destructor
};
Changed the variable name to original, but the error is still the same.
class CellPhoneHandler
{
private:
CellPhone **cellphone;
int nrOfCellphones;
public:
CellPhoneHandler();
CellPhoneHandler(const CellPhoneHandler &original);
CellPhoneHandler operator=(const CellPhoneHandler &original);
void addPhone(string model, int price, int stock);
~CellPhoneHandler();
string showByStock(int stock) const;
void removeModel(string model);
void changePriceProcent(double procent, int price);
void showAll(string array[], int nrOfCellphones) const;
void saveToFile(string fileName) const;
void readFromFile(string fileName);
int getNrOfPhones()const;
};
UPDATE: changed my operator= to this, simplified code:
CellPhoneHandler CellPhoneHandler:: operator=(const CellPhoneHandler &original){
CellPhoneHandler tmp(original);
swap(this->cellphone, tmp.cellphone);
swap(this-> nrOfCellphones, tmp.nrOfCellphones);
return *this;
}
the program works now, however, is this one copying in depth? My teacher told me my last assignment did not do that.