0

I have a list of pointers to objects. What I want to do is read the list and store each object in a dynamic array of that object type. So I do this:

int size = List.size();  // find how many objects in the list
ClassA* object = new ClassA[size];   // create a dynamic array
int counter = 0;
p = List.begin();   // iterator = begining of the list
while( p != List.end() )
{
    object[counter] = (*p)->operator=(*(*p));
                // called like this as this function is in a separate class
    p++;
    counter++;
}

This appears to be what I want which means I need the assignment operator but I am a bit confused as to what to put in it and I am getting stack overflow errors, here is what I thought I needed to do:

 ClassA ClassA::operator =(const ClassA& source)
 {
     ClassA* newObject;
     newObject = new ClassA;
     newObject = source;
     return newObject;

 }

this is a load of BS I no that but in my head this is what I want it to do but I don't fully understand how I implement it.

If anyone can help or suggest a better way to achieve what I need it would be appreciated.

The reason for doing this is the objects stored on this list are normally iterated through each frame and copied into a temporary object. But the list is not changed during the running of the program, which means I don't need to copy each frame, only once at the start. And I need a dynamic array because I don't know how many objects will be stored in the list.

2
  • Does your list contain pointers, or actual objects? (ie list<ClassA> or list<ClassA*>)? Commented Apr 29, 2012 at 16:45
  • Sorry should have made that clearer David Brown it contains a list of pointers Commented Apr 29, 2012 at 16:46

1 Answer 1

2

This could be an operator= implementation:

ClassA &ClassA::operator =(const ClassA& source) {
    // check for self-assignment
    if(this != &source) {
        // copy instance variables.
        a = source.a; // for example...
    }
    // assignment always returns the lvalue
    return *this;
}

I don't know your ClassA's instance variables, so you should implement the internal copying.

On the other hand, when iterating your list, you can copy objects this way:

object[counter] = (**p);

(**p) returns, first the pointer stored in the iterator, and then dereferences it.

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

4 Comments

would that mean i would have to copy each variable of the instance , lets say the class has 30+ variables then a =source.a b = source.b and so on
Yes, you would have to copy each variable. Anyway, there is a default assignment operator which does exactly that. You should only implement this operator if your class handles resources internally, like pointers, devices, etc.
that worked a treat thank you very much fontanini, can you just explain a bit more about handling the resources internally if you don't mind, i don't quite follow ?
I'm glad it helped. You can read this stackoverflow thread which explains when you should implement copy assignment operator and the "rule of three": stackoverflow.com/questions/4172722/what-is-the-rule-of-three

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.