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?
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:
and:
On the positive side it says:
So your choice will depend on how relevant the negative issues are for your application.
You are making use of the overloading feature, if you provide several
constructor
s. The advantage in this case is, that you can react differently in everyconstructor
on the passed arguments. If that is of importance use overloading.If you can provide decent
default
values for your parameters and these wouldn't affect the proper running of your code, use default parameters.See here for a thread on SO.
The compiler doesn't care which of these you use. Imagine that you wrote it as two constructors, and they ended up about 20 lines long. Further imagine that 19 of the lines were identical, and the different line read
in one version and
in the other. In this situation, using an optional parameter makes your code far more readable and understandable. In another language, that didn't have optional parameters, you'd implement this by having the one-parameter version call the two parameter version and pass zero to it as the second parameter.
Now imagine a different pair of constructors or functions that again are about 20 lines long but are entirely different. For example the second parameter is an ID, and if it's provided, you look stuff up in the database and if it's not you set values to
nullptr
, 0, and so on. You could have a default value (-1 is popular for this) but then the body of the function would be full ofwhich could be hard to read and would make the single function a lot longer than the two separate functions. I've seen functions with one giant
if
that eseentially split the whole thing into two separate blocks with no common code, and I've seen the same condition tested 4 or 5 times as a calculation progresses. Both are hard to read.That's the thing about C++. There are lots of ways to accomplish most things. But those different ways serve different purposes, and once you "get" the subtle differences, you will write better code. In this case "better" means shorter, faster (all those
if
s cost execution time), and more expressive - people reading it can understand your intentions quickly.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.