0

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?

Bart
  • 19,692
  • 7
  • 68
  • 77
HungryCoder
  • 1,029
  • 2
  • 10
  • 16
  • 1
    Can you be more specific? What does "doesn't work" mean? It would be *very* helpful if you could reduce your program to the smallest complete sample program that demonstrates your problem. See http://sscce.org/. – Robᵩ May 06 '12 at 01:24
  • By doesn't work something really weird happens. Basically, you might call it a time limit exceed or an infinite loop. Program execution halts and I get to see a blinking cursor on my console. i gave out the whole bit, since my problem involves public/private issues. – HungryCoder May 06 '12 at 01:27
  • Note that `ArrayStorage arrayStorage4 = arrayStorage3;` should call the constructor, not `operator=`. – chris May 06 '12 at 01:34
  • Yes, I do have the other two as well, the copy constructor, destructor & over here operator – HungryCoder May 06 '12 at 01:39
  • 3
    `private` vs `public` is a red herring, unrelated to the actual problem. Please reduce your program to the smallest **complete** compilable program that demonstrates the problem and post that here. See http://sscce.org/. – Robᵩ May 06 '12 at 01:57
  • Nevermind. Thankyou for helping. I have given up. – HungryCoder May 06 '12 at 02:10

1 Answers1

1

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)

Klemens Baum
  • 551
  • 3
  • 14
  • 2
    I think you mean [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). The [Law of Demeter](http://en.wikipedia.org/wiki/Law_of_Demeter) is something else. – Robᵩ May 06 '12 at 01:26
  • Yes rule of 3. And sorry can't use a std::vector – HungryCoder May 06 '12 at 01:28
  • Why can't you use a std::vector? – Klemens Baum May 06 '12 at 01:34
  • it is a part of a coursework, we were specifically asked to use a c-style array – HungryCoder May 06 '12 at 01:35
  • @RashedHassan, there's a tag for that. It's one of the important ones to put on. – chris May 06 '12 at 01:35
  • @chris - which tag? Rule of 3? Can any one answer my 2nd part of the question? Making it work with pvt declaration of the string *names? – HungryCoder May 06 '12 at 01:37
  • @RashedHassan, the homework tag. I added it for you. It influences the types of answers people will give and eliminates the need for people to ask when the question sounds like homework, but they can't know for sure. – chris May 06 '12 at 01:43