When should we use method overloading vs method with different naming?

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

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

5 method is a language agnostic term and refers to subroutines/functions associated with a class in object oriented programming. The usage here is correct. – bshields Aug 12 '10 at 4:04.

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

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.

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.

What a nightmare that would be! – GMan Aug 12 '10 at 3:45 Yeah clearly that would not be good, I think the example you posted is more along the lines of what people worry about. If the overload really makes it so that the user doesn't have to worry about what type it is because the right thing is going to get done either way then that's good, but if there is different behavior that happens based on what type it is that's where things can get subtle.

As you point out this is really an abuse of overloading. – bshields Aug 12 '10 at 3:48.

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 (e.g. 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.

We should use in T. We should use on a. We use method overloading vs method with different naming in (.we use method overloading vs method with different naming on (.method overloading vs method with different naming we should in (.method overloading vs method with different naming we should on (.We should in S.

We should on p.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions