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?

6
  • 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 sscce.org. Commented May 6, 2012 at 1: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. Commented May 6, 2012 at 1:27
  • Note that ArrayStorage arrayStorage4 = arrayStorage3; should call the constructor, not operator=. Commented May 6, 2012 at 1:34
  • Yes, I do have the other two as well, the copy constructor, destructor & over here operator Commented May 6, 2012 at 1: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 sscce.org. Commented May 6, 2012 at 1:57

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

7 Comments

I think you mean Rule of Three. The Law of Demeter is something else.
Yes rule of 3. And sorry can't use a std::vector
Why can't you use a std::vector?
it is a part of a coursework, we were specifically asked to use a c-style array
@RashedHassan, there's a tag for that. It's one of the important ones to put on.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.