Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pass pointer by reference and assign default value

I would like to pass a pointer by reference to a function, such that i can actually change the address the passed pointer is pointing to and i'd like to assign this argument a default value.

something like this:

in the declaration

void myFunc(SomeType* &var=NULL);

and the definition:

void MyClass::myFunc(SomeType* &var){
    if(var!=NULL)
        (*var)=(*someOtherPointer);

    if(someCondition)
        var=NULL;
}

such that a callee can decide whether he wants to call the function with one argument or without argument. And sucht that if he decides to pass an argument, and someCondition holds, the passed pointer will point to NULL afterwards

however - if i try to do it like this i get a:

Error C2440: 'default argument': 'int' cannot be conveted to 'SomeType *&'

Thanks for the help!

like image 488
Mat Avatar asked Dec 02 '09 01:12

Mat


People also ask

Is C pass by reference or pass by value?

C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.

What happens when you pass a pointer by reference?

In pass-by-reference, a pointer is passed into the function. The caller's copy could be modified inside the function. In pass-by-reference with reference arguments, you use the variable name as the argument.

Is pass by reference allowed in C?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

Do pointers pass by reference or value?

Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied. That means that if you change the value of the pointer in the function body, that change will not be reflected in the external pointer that will still point to the old object.


3 Answers

NULL is not an lvalue - it cannot be passed by reference. It would be like passing 4 to a function that expects an int&.

The 'int' part is because NULL is a macro - defined 0.

Your best bet would be using a pointer to pass the pointer by reference, i.e. a double pointer. Then, if the parameter is NULL, nothing was passed. If not, it's the address of the pointer that should be modified [to point to NULL if someCondition holds].

like image 195
aib Avatar answered Sep 19 '22 22:09

aib


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;
}
like image 29
cwick Avatar answered Sep 23 '22 22:09

cwick


You can also consider using boost::optional (not the simplest code you can use, but the option is there):

void f( boost::optional<int&> r = boost::optional<int&>() )
{
   if ( r ) *r = 5;
}
int main()
{
   int x = 0;
   f( x ); std::cout << x << std::endl; // 5, it did change the value
   f(); // compiler will default to an empty optional<int&> object
}
like image 42
David Rodríguez - dribeas Avatar answered Sep 21 '22 22:09

David Rodríguez - dribeas