Using temporary arrays to cut down on code - inefficient?

Posted by tommaisey on Stack Overflow See other posts from Stack Overflow or by tommaisey
Published on 2012-11-17T04:18:42Z Indexed on 2012/11/17 5:00 UTC
Read the original article Hit count: 110

Filed under:
|
|

I'm new to c++ (and SO) so sorry if this is obvious.

I've started using temporary arrays in my code to cut down on repetition and to make it easier to do the same thing to multiple objects. So instead of:

MyObject obj1, obj2, obj3, obj4;

obj1.doSomming(arg);
obj2.doSomming(arg);
obj3.doSomming(arg);
obj4.doSomming(arg);

I'm doing:

MyObject obj1, obj2, obj3, obj4;
MyObject* objs[] = {&obj1, &obj2, &obj3, &obj4};

for (int i = 0; i !=4; ++i)
    objs[i]->doSomming(arg);

Is this detrimental to performance? Like, does it cause unnecessary memory allocation? Is it good practice? Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays