Home > OS >  C pass a non const string by reference with default value
C pass a non const string by reference with default value

Time:06-06

So I am working on a codebase where this someFunc is called at a lot of places and I can't afford to change it by adding this new variable at all places where this function is called. And that too when that variable is not needed at all the place where someFunc is called.

So I have a requirement where I need to have a function definition having a string which is pass by reference and we are able to mutate the value of the same.

  • Pass by reference: because that string variable is used by the caller
  • Default value : Because this func is called at lot of place, can't change everywhere.
  • Non const : Because I want to mutate it

This is what I have tried :

ExceptionClass ClassName::someFunc(int a, float b, map<int, string> c, const string &d = "")

The problem with this approach is I am unable to change the value of d inside my function. Getting following error :

enter image description here

How do I use something that fulfills my requirement?

CodePudding user response:

You can get the equivalent behavior that you are describing by using a separate overload instead of a default value:

class ClassName {
public:
  ExceptionClass someFunc(int a, float b, map<int, string> c, string &d);
  ExceptionClass someFunc(int a, float b, map<int, string> c) {
    string d = "";
    return someFunc(a, b, c, d);
  }
};

CodePudding user response:

Assigning "" as default argument makes no sense. If you are modifying the argument then those changes seem to have some value and that would be lost with a string literal.

I can think of two ways that could make sense:

static string someFuncDefault = "";
ExceptionClass ClassName::someFunc(int a, float b, map<int, string> c, string &d = someFuncDefault);

or

ExceptionClass ClassName::someFunc(int a, float b, map<int, string> c, optional<reference_wrapper<string>> d_opt = {});

The first case would have a default argument that persists across function calls. So modifications from one call will change the default use for the next call.

The second makes the string optional but in a way the code can check if it was passed or not and behave accordingly. No point modifying the string if it wasn't passed along.

But really, why don't you have 2 functions? One with 3 and one with 4 arguments?

CodePudding user response:

You can also use string pointer that accepts a default empty string.

class ClassName {
public:
  ExceptionClass someFunc(int a, float b, map<int, string> c, string *d = new string());
  }
};

and wherever you are calling this function you can pass the address of your string variable

string d = getD();
yourObj.someFunc(a, b, c, &d);

Your function definition can then use * operator to dereference the pointer variable.

ExceptionClass ClassName ::someFunc(int a, float b, map<int, string> c, string *d)
{
    *d = doSomething();
}

CodePudding user response:

Why using const if you want to mutate the string. Try this version:

ExceptionClass ClassName::someFunc(int a, float b, map<int, string> c, string &d)
  • Related