Overload a method or use default values? c++

2019-04-04 07:32发布

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?

4条回答
smile是对你的礼貌
2楼-- · 2019-04-04 08:10

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.

查看更多
老娘就宠你
3楼-- · 2019-04-04 08:11

You are making use of the overloading feature, if you provide several constructors. The advantage in this case is, that you can react differently in every constructor 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.

查看更多
Fickle 薄情
4楼-- · 2019-04-04 08:16

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

foo = 0;

in one version and

foo = optional;

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 of

if (ID == -1)
{
    foo = 0;
}
else 
{
    foo = DbLookup(ID);
}

which 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 ifs cost execution time), and more expressive - people reading it can understand your intentions quickly.

查看更多
疯言疯语
5楼-- · 2019-04-04 08:17

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.

查看更多
登录 后发表回答