Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
Cheok Yan Cheng Avatar asked Aug 12 '10 03:08

Cheok Yan Cheng


People also ask

What is the difference between overloading a method name and overriding a method name?

Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.

When should method overloading be used?

Overloading is a powerful feature, but you should use it only as needed. Use it when you actually do need multiple methods with different parameters, but the methods do the same thing. That is, don't use overloading if the multiple methods perform different tasks.

Is method overloading depends on the name of the method?

Method overloading in java is based on the number and type of the parameters passed as an argument to the methods. We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error.

Does method overloading allow same member name with different parameters?

Method overloading in java is a feature that allows a class to have more than one method with the same name, but with different parameters.


4 Answers

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.)

like image 83
GManNickG Avatar answered Oct 09 '22 16:10

GManNickG


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); 
}
like image 27
kyku Avatar answered Oct 09 '22 17:10

kyku


If you're essentially doing the same thing with the two functions, they just differ in their type arguments, then it might make more sense to use templates and not use overloading at all.

like image 37
andand Avatar answered Oct 09 '22 17:10

andand


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++.

like image 23
scott77777 Avatar answered Oct 09 '22 17:10

scott77777