Default argument in a function [C++]?

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).

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).

Thanks guys for all your answers. – There is nothing we can do Dec 14 '09 at 10:13 +1 But the signature indicates that the OP probably want's to modify number, which won't be possibily without casting away the const. – Andreas Brinck Dec 14 '09 at 10:14 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. – anon Dec 14 '09 at 10:16 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).

– stefaanv Dec 14 '09 at 12:15 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. – haavee Dec 14 '09 at 12:16.

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?

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); }.

Your idiom is wrong. A global function should basically never return a reference - other than e.g. 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions