简体   繁体   中英

When should we use method overloading vs method with different naming

Sometimes, I felt method overloading may create confusion.

class a {
public:
    void copy(float f);
    void copy(double d);
};

a me;
me.copy(1.2); // Not obvious at the first sight on which version we are calling.

A workaround on this is.

class a {
public:
    void copyFloat(float f);
    void copyDouble(double d);
};

However, having method with different name, to perform same functionality doesn't seem a good idea as well. May I know, what do you consider, to choose among method overloading , or method with different naming ?

Overloading for sure.

Okay, so it's not "obvious" which function gets called (arguable)...so what? You don't care that it can take different types of parameters, it just needs to do its thing. If you have different behavior based on different overloads, you've abused overloads, not pointed out a flaw in them.

An example of abusing overloads:

// good:
struct has_properties
{
    void property1(float); // set property1, which happens to be a float
    void property2(int); // set property2, which happens to be an int
};

// bad:
struct has_properties
{
    void property(float); // set property1, abusing that it's a float
    void property(int); // set property2, abusing that it's an int
};

Hopefully you see the problem here. If two functions have the same name, they should do the same thing.

Even better, if you're merely trying to allow the possibility for operating on different types, just use a template. (This arguably is a form of overloading.)

I would put this in a comment, but I can't yet. Since you tagged this as C++, I thought I should tell you that methods are usually called functions in C/C++.

Try to avoid using multiple virtual methods with the same name. Or you will probably want to override them all in derived classes. Look at the following example:

#include <string>

struct Base {
    virtual void foo(const std::string& arg) {
    }
    virtual void foo(int arg) {
    }
};

struct Derived : Base {
    // Only std::string version is overriden.
    virtual void foo(const std::string& arg) {
    }
};

int main() {
    Derived d;
    // This fails to compile because the name lookup stops
    // after Derived::foo has been found.
    d.foo(42); 
}

如果你基本上对这两个函数做同样的事情,它们的类型参数只是不同,那么使用模板可能更有意义,而根本不使用重载。

Certainly in the case of float vs. double you should not overload, it's just too error prone. I've heard people argue that you should always use different names in cases like these just to be completely explicit and make things as clear as possible to the reader and I tend to agree.

Overloaded functions is the C++ approach to grouping functions which are similar on their behavior.

do_it (A a, B b);
do_it (B b, int i = 0);

Functions with different names (eg do_it_with_a or do_it_with_a_b) is the C approach to grouping functions which are similar on their behavior.

So if your functions are different in their behavior, don't overload them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM