3

I tried to do something like this:

int& g(int& number = 0)
{
//maybe do something with number
    return number;
}

but it doesn't work. It has to be passed by reference. Thank you for any help.

P.S. I think that "Related Questions" appearing once you type Title is a good idea, but I also think that they should be displayed only if they are related to specific language, i.e. it is less than useless for me to looking at topic with similar problem but in Ruby.

1
  • What do you want to achieve? (Saying: I typed in these words and they don't work doesn't ring a bell...) Commented Dec 14, 2009 at 10:07

4 Answers 4

11

If you really want to do this:

  • make the reference const, so that a temporary can be bound to it
  • put the default in the function declaration, not the definition

For example:

// header
const int & g( const int & number = 0 );


// implementation
const int & g( const int & number )
{
//maybe do something with number
    return number;
}

PS Post your complaints about how SO works on Meta, not here (for all the good it will do - answering this question indicated they STILL haven't fixed the code-after-list bug)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks guys for all your answers.
+1 But the signature indicates that the OP probably want's to modify number, which won't be possibily without casting away the const.
No he won't be. But casting away the const makes no sense in this case - if he wants default int& parameters, he has to resign himself to them being const.
It is a bit a funky construction anyway, but couldn't "const int & i = g();" lead to a dangling reference? If I wanted to return something, I'd return a value (even when returning a class).
This is not good! Is isn't even a solution since even the temporary's storage will be out of scope after the function g() returns, leaving a dangling reference.
|
4

You have a non-const reference, which means you can modify the referand. But your default is the constant 0.

Does it really make sense for this function to have a default?

Comments

0

A non-const reference cannot bind to a temporary (literal).

Here it would probably make most sense to take arguments and return by value, and leave it up to the caller to assign the result back to the variable (if they want so): k = g(k);

Perhaps you could also use two overloads, but the "default" one cannot return by reference, which might lead to some confusion:

int& g(int&);

int g()
{ 
    int arg = 0;
    return g(arg);
}

Comments

0

Your idiom is wrong.

A global function should basically never return a reference - other than e.g. hiding a global variable. That case is acceptable, however, the usage is questionable: if the variable was already global, why hide it behind a functioncall?

If your goal is to modify the argument, then why give a returnvalue? Only usefull if you e.g. return the old value - and that has to be done "by value" rather than "by reference".


int g(int& arg) {
   int oldarg( arg );
   // maybe modify arg
   // return old value of arg
   return oldarg;
}

or:


const int& g(int& arg) {
    static int accumulator;
    accumulator += arg;
    return accumulator;
}

Where, in the latter case, passing argument by reference and/or returning the accumulator by reference is unnecessary.

cheers, h.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.