Is using a reference parameter with default value good practice?
It's not a horrible practice, but it's generally better to provide overloads:
void f(std::string const& s) { std::cout << "\\" << s << "\\\n"; }
void f() { f(""); }
It avoids some language features that end up being confusing to many people. For example, what does this print?
struct base { virtual void f(int i = 42) { std::cout << i; } };
struct derived : base { void f(int i = 19) { std::cout << i; }};
int main() { base * b = new derived(); b->f(); }
There are also ambiguity errors that can come up when you're using default parameters that don't when you use overloads.
As far as const references in particular, that doesn't really matter much. The default value binds to reference for the lifetime of the function call. It has no effect at all really. You might get better results using values sometimes when the compiler can perform certain optimizations that are not possible with reference parameters, but generally it's not something to be concerned with.
Of course, this doesn't work with non-const references because they don't bind to temporaries.
Passing optional parameter by reference in c++
The default argument of a (mutable) reference must be an l-value. The best I can think of, without overloading, is
static double _dummy_foobar;
void foo(double &bar, double &foobar = _dummy_foobar)
C++ pass a non const string by reference with default value
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);
}
};
C++ pass pointer by reference and assign default value
The error message says it all: you are passing an integer instead of a reference-to-a-pointer-to-SomeType. To do what you want, you can use a pointer-to-a-pointer-to-SomeType:
void myFunc(SomeType** var=NULL);
void MyClass::myFunc(SomeType** var){
if(var!=NULL && *var!=NULL)
(**var)=(*someOtherPointer);
if(var!=NULL && someCondition)
*var=NULL;
}
Related Topics
Win32 API to Enumerate Dll Export Functions
(-2147483648≫ 0) Returns True in C++
Combining Several Static Libraries into One Using Cmake
Differences Between C++ String == and Compare()
Why Does Pointer Decay Take Priority Over a Deduced Template
How to Use Stringstream to Separate Comma Separated Strings
C++ Standard: Dereferencing Null Pointer to Get a Reference
C++ Static Member Method Call on Class Instance
Why Dereferencing a Null Pointer Is Undefined Behaviour
Do I Need to Manually Close an Ifstream
Is Is_Constexpr Possible in C++11
Is It a Conforming Compiler Extension to Treat Non-Constexpr Standard Library Functions as Constexpr
Cout Not Printing Unsigned Char
C++ Semantics of 'Static Const' VS 'Const'
Is Std::Vector or Boost::Vector Thread Safe
How to Check If Input Is Numeric in C++
Atomic Double Floating Point or Sse/Avx Vector Load/Store on X86_64