Re: Overloaded functions

From: Stephen M. Webb (stephen.webb_at_bregmasoft.com)
Date: 11/27/03


Date: 27 Nov 2003 08:18:53 -0800


"kazack" <kazack@talon.net> wrote in message news:<LXkxb.7429$Bv6.2253651@news1.epix.net>...
> I know that this is possible and I now how it is possible.
> But why would you want to call more than one function the same name to begin
> with? Wouldn't it be just easier to call them 2 different names?
>
> Ex.
> #include <string>
> #include <iostream>
> //why not integer_print and string_print??????
> void print(int);
> void print(string);
>
> int main()
> {
> string name = "test";
> int a = 4;
> print(a);
> print(name);
> return 0;
> }
>
> void print(string name)
> {
> cout << name;
> }
>
> void print(int a)
> {
> cout << a;
> }

It comes in particularly handy when used with operator overloading.
For example, you wanted to overload operator<< to handle, say, a
string and an int differently. You can't rename the operator, but you
can overload it for te two types.

As well, in C++, the types of the parameter form a part of the
function name (which may, in your view, be a chicken-and-egg type
problem). Adding the type to the name explicitly, as in print_string
and print_int, is redundant repetition of the same information.

Consider, also, the interaction of function overloading and templates.
 If you could not overload a function, how could you template it?

--
Stephen M. Webb


Relevant Pages

  • Global overload of new and delete ---- Borland feature only?
    ... When arrays of class objects are created with new, ... To overload, you pass two arguments to new: ... The type of the first argument is void *. ... Do not use delete to destroy a class object that has been ...
    (comp.lang.cpp)
  • Re: Help with generic method definition containing BitConverter.GetBytes
    ... void Write{ ... It won't compile for any T. The only .GetBytesoverload that can legally be invoked for a generic T would take a parameter of Object type, ... If you take the step of optimizing the reflection the code becomes even more unclear, and might actually end up longer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overloading a method with different return types but the same parameter lists
    ... void GetRoles(string username, out ArrayList al); ... > 'GetRoles' with the same parameter types ... > How can I overload these methods to provide the functionality that I want. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: OnDocumentComplete event on a derived CHtmlView
    ... void CTestChtrmlViewView::OnDocumentComplete(LPCTSTR lpszURL) ... but on the microsoft's web site, the overload is like this: ... void CTestChtrmlViewView::OnDocumentComplete(LPDISPATCH lpDisp, VARIANT ...
    (microsoft.public.vc.mfc)
  • Re: optional parameter in function definitions?
    ... Chris Shepherd wrote: ... Overload the function. ... void MyMethod ... I would say that the reply from "pedrito" is more suitable in what one would consider a "variable parameter list". ...
    (microsoft.public.dotnet.languages.csharp)