views:

39

answers:

1

I have a function similar to this:

void foo(obj ary[], int arysize) {
    for (int i = 0; i < arysize; i++)
         ary[i] = obj(i, "abc");
}

And I call it like this:

obj array[5];
foo(array, 5);

It's supposed to populate the array with my objects. However, when it returns, the objects are garbage. It works with value types like int and stuff, so I think it's something to do with the object I create being local in scope so it gets destroyed when the function returns. I would like to be able to do this without using dynamically allocated objects with new. How can I do this?

+2  A: 

That should work fine. I'd look into the assignment operator for your class.

James Curran
Wow how stupid of me. The assignment operator was changing the attributes of the object but not copying the data over. Thanks.
Thomas T.