开发者

Filling an array passed as a parameter with objects

开发者 https://www.devze.com 2023-01-13 02:14 出处:网络
I have a function similar to this: void foo(obj ary[], int arysize) { for (int i = 0; i < arysize; i++)

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 thi开发者_Go百科nk 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号