Passing Optional Parameter by Reference in C++

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)

How to pass optional arguments to a method in C++?

Here is an example of passing mode as optional parameter

void myfunc(int blah, int mode = 0)
{
if (mode == 0)
do_something();
else
do_something_else();
}

you can call myfunc in both ways and both are valid

myfunc(10);     // Mode will be set to default 0
myfunc(10, 1); // Mode will be set to 1

C++ - Optional arguments by reference

I don't know if that is what the question was aiming at, but temporaries can be bound to const references:

double volume_prisma(const double& largura, ..., const double& depth = 10);

C intro - How to pass a parameter by reference in function?

  1. It is perfectly valid. You can initialize and pass any number of pointer variables with their reference.

  2. This is also valid..when you pass the variable address, you should store it into a pointers

you have to do some changes in your code,
You can assign directly a/b and a*b pointer variables *c & *d
Then you have to read double number with %lf format argument.

#include <stdio.h>
#include <string.h>

void myFunction(double a, double b, double *c, double *d)
{
*c = a/b; //change
*d = a*b; //change
printf("%lf %lf",*c,*d);
return;
//printf statements
}

int main()
{
//first and second double hold the scanf inputs
double first;
double second;

//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;

printf("Enter your first number\n");
scanf("%lf", &first); //change
printf("Enter your second number\n");
scanf("%lf", &second); //change

//call the function, first and second by value, &c / &d by reference - correct?
myFunction(first, second, &c,&d);
}

Optional parameter for reference to pointer?

You could just overload the function

template <typename T>
inline void Delete (T *&MemoryToFree){
delete MemoryToFree;
MemoryToFree = NULL;
}

template <typename T, typename T2>
inline void Delete (T *&MemoryToFree, T2 *&MemoryToFree2){
delete MemoryToFree;
MemoryToFree = NULL;

delete MemoryToFree2;
MemoryToFree2 = NULL;
}

Modern C++ approach for providing optional arguments

Accepting a raw pointer is perfectly fine and is still done in plenty of "modern" codebases (which I'll note is a fast-moving target). Just put a comment on the function saying that it's allowed to be null and whether the function holds a copy of the pointer after the call (i.e. what are the lifetime requirements for the pointed-to value).

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


Related Topics



Leave a reply



Submit