-2

If I have a function declaration as follows:

int Remove(Object *spl, Object1* parent,
           int num, Object2* th = NULL, bool& proceed
          );

I get an error that I need to declare 'proceed' becasue 'th' has a default argument. I understand why. But how do I make a default argument for a "bool&" in function declaration?

ssn
  • 2,631
  • 4
  • 24
  • 28

2 Answers2

1

All function in C++ have to end with the default arguments. It is not possible to call your function like so:

Remove(someSpl, someParent, 10, /* no argument, use default */, someBool);

Since you cannot omit arguments in the middle, they have to wander to the end.

Regarding the first comment clarifying the question, let me give a small example that should transfer fine to your application:

void changingBoolFunction(bool& someBool)
{
    someBool = false;
}

bool changeableBool = true;
changingBoolFunction(someBool);
cout << (changeableBool ? "true" : "false"); // output: false
IceFire
  • 4,016
  • 2
  • 31
  • 51
  • I understand that. Let me rephrase my question. How do I initialise 'bool&' in: int Remove(Object *spl, Object1* parent, bool& proceed, int num = 0, Object2* th = NULL ); – ssn Mar 16 '16 at 22:11
1

To clarify: that value for th is a default argument, not an initialization. And once you've put in a default argument every argument after that one has to have a default. It's unusual to have a default value for an argument that's passed by (modifiable) reference, but if that's what you really want, you have to provide a bool object for that default argument:

bool bool_value = true;
int Remove(Object *spl, Object1* parent,
           int num, Object2* th = NULL, bool& proceed = bool_value
          );

After the function returns, if it changed the value of proceed that change will show up in bool_value.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165