If you want to call a member function in a recursive manner, which one is preferred?
Case 1) instantiate the 'C', 'a' times.
void foo (int a) {
if (a==0) return;
Car C;
C.memberfunc(a);
...
foo(a-1);
}
Case 2) instantiate the 'C' once, and uses its member function in the recursive foo
Car C;
void foo (int a) {
if (a==0) return;
C.memberfunc(a);
...
foo(a-1);
}
UPDATE: There is no state here. So, according to Difference between declaring variables before or in loop? Case 1) should be better. But the answers imply that Case 2) is the better option.
Can you tell me which approach is better and why? Thanks!