19

I want to set my function with class object parameter set as default. But when I try to do that it fails in compilation.

class base {
 // ...
};

int myfunc(int a, base b = NULL) {
    if (NULL = b) {
        // DO SOMETHING
    } else {
    // DO SOMETHING
    }
}

Here when i am trying to compile it, this gives me error that "Default Argument base b have int type"

1
  • 2
    You need to make b a pointer to base for this to work: base* b = NULL Commented Aug 25, 2012 at 11:47

4 Answers 4

28

Objects can't be NULL in C++.

To set the parameter to default, just use:

int myfunc(int a, base b = base())
Sign up to request clarification or add additional context in comments.

3 Comments

Is base() calling base's constructor?
@David yes, base() default-constructs an object of type base.
In this case how would one check if an argument was supplied since base can't be compared to NULL?
20

You have three obvious options here.

First, use overloads so the caller can choose to pass b or not.

int myfunc(int a) { ... }
int myfunc(int a, base& b) { ... }

This way you can pass b without having to use a pointer. Note that you should make b a reference or pointer type to avoid slicing the object.

Secondly, if you don't want 2 separate implementations, make b a pointer, which can be set to NULL.

int myfunc(int a, base* b = NULL) { ... }

Third, you could use something to encapsulate the concept of nullable, such as boost::optional.

int myfunc(int a, boost::optional<base&> b = boost::optional<base&>()) { ... }

Comments

1

@tenfour answer forgot to mention another possible way. You also define a global variable object which you can construct as you like, then set it as the default value:

#include <iostream>

class MyCustomClassType
{
  int var;

  friend std::ostream &operator<<( 
        std::ostream &output, const MyCustomClassType &my_custom_class_type )
  {
    output << my_custom_class_type.var;
    return output;
  }
};

// C++11 syntax initialization call to the default constructor
MyCustomClassType _my_custom_class_type{};

void function(MyCustomClassType my_custom_class_type = _my_custom_class_type) {
  std::cout << my_custom_class_type << std::endl;
}

/**
 * To build it use:
 *     g++ -std=c++11 main.cpp -o main
 */
int main (int argc, char *argv[]) {
  function();
}

Comments

0

A hack or ugly solution would be do a static cast from null:

#include <iostream>

class MyCustomClassType {
  int var;

  friend std::ostream &operator<<( 
         std::ostream &output, const MyCustomClassType &my_custom_class_type )
  {
    output << my_custom_class_type.var;
    return output;
  }
};

void function(
       MyCustomClassType my_custom_class_type = *static_cast<MyCustomClassType*>( nullptr )
    ) 
{
  std::cout << my_custom_class_type << std::endl;
}

/**
 * To build it use:
 *     g++ -std=c++11 main.cpp -o main
 */
int main (int argc, char *argv[]) {
  function();
}

But, running this gives you directly a segmentation fault due dereferencing a null pointer. I am not sure when this would be useful.

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.