How to keep the parameter the same in gtest

Asked 1 month ago
Viewed 14 times

Suppose the function

bool foo(int& num) {
     // do something and change the num
     return true;

Want the num to be the same before and after calling foo without caring what happens in foo.

asked 1 month ago

Correct Answer

You should not pass the parameter "num" as reference "&". Pass it as value: bool foo(int num) {...} num outside foo() will stay the same.

Here is more about passing by reference:

answered 1 month ago

Other Answer

Solution 1:

Copy the parameter value:

int prev_num = num;
num = prev_num;

Solution 2: Use a wrapper

// Wrapper uses pass by value for num
bool fooWrapper(int num) {
  return foo(num);

// Then later use the fooWrapper instead of foo:

answered 1 month ago