Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload a method or use default values? c++

Tags:

I'm still relatively new to C++ and I can't seem to figure out the difference in the following two ways of coding a function that may take one parameter or maybe two or three or more. Anyway, here's my point

function overload:

int aClass::doSomething(int required)
{
    //DO SOMETHING
}

int aClass::doSomething(int required, int optional)
{
    //DO SOMETHING
}

how is this different to, default value:

int aClass::doSomething(int required, int optional = 0)
{
    //DO SOMETHING
}

I know in different circumstances one may be more suitable than another but what kinds of things should I be aware of when choosing between each of these options?

like image 913
AngryDuck Avatar asked Apr 08 '13 10:04

AngryDuck


People also ask

Can we overload function with default argument?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.

Can you overload method in C?

And C doesn't support Function Overloading.

What is used in function overloading and function with default argument?

Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++. Explanation: Both of the features allow one function name to work for different parameter.

What type are C functions by default?

By default, C uses call by value to pass arguments.


2 Answers

There are several technical reasons to prefer overloading to default arguments, they are well laid out in Google's C++ Style Guide in the Default Arguments section:

Function pointers are confusing in the presence of default arguments, since the function signature often doesn't match the call signature. Adding a default argument to an existing function changes its type, which can cause problems with code taking its address. Adding function overloads avoids these problems.

and:

default parameters may result in bulkier code since they are replicated at every call-site -- as opposed to overloaded functions, where "the default" appears only in the function definition.

On the positive side it says:

Often you have a function that uses default values, but occasionally you want to override the defaults. Default parameters allow an easy way to do this without having to define many functions for the rare exceptions.

So your choice will depend on how relevant the negative issues are for your application.

like image 185
Shafik Yaghmour Avatar answered Oct 06 '22 20:10

Shafik Yaghmour


First off, you're talking about overloading, not overriding. Overriding is done for virtual functions in a derived class. Overloading refers to the same function name with a different signature.

The difference is logical - in the first case (2 versions), the two functions can behave completely different, whereas the second case will have more or less the same logic. It's really up to you.

like image 20
Luchian Grigore Avatar answered Oct 06 '22 21:10

Luchian Grigore