Reputation: 1129
Declared in arraystorage class, private: string *names;
ArrayStorage& ArrayStorage::operator=(const ArrayStorage& rhs)
{
// possible error
names = new string[numOfElements];
return *this;
}
// copy constructor
ArrayStorage::ArrayStorage(const ArrayStorage& rhs):
names(new string[numOfElements]),
numOfElements(rhs.numOfElements)
{
//names = new string[this->getNumOfElements()];
for (int i = 0; i < this->getNumOfElements(); i++)
names[i] = rhs.names[i];
}
ArrayStorage::~ArrayStorage(void)
{
delete [] names;
}
================================ ArrayStorage.cpp==============================
My first problem, if I declare names as private, the whole thing doesn't work. It works if I put it as public.
Secondly, can you please advise, how do I make it work, if I want to declare string *names
as private?
Upvotes: 0
Views: 569
Reputation: 561
Use a RAII-aware class like std::vector<std::string>
and drop the assignment operator.
Furthermore, you may want to read up on the Law of Three (if you have either of destructor, copy assignment operator, copy constructor; then you should have all of them).
(edit: fix law name)
Upvotes: 1