function overloading

This is a discussion on function overloading within the C++ Programming forums, part of the General Programming Boards category; ok i know what this does...it allows to functions of the same name have different data,but why would i use ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    function overloading

    ok i know what this does...it allows to functions of the same name have different data,but why would i use this? why not just create a different function name

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Function overloading can be very handy. If you were creating say a game, you could use two functions with the same name but with different arguements say for two enemys. But you could still call both functions Enemy().

    Also, in operator overloading, you can make the standard C++ operators do differnet things, example, you could make the '+' operator subtract for example. But messing with logical operators is somthing I am always keen to stay away from. If an operator was built to do a certaun thing, why change it?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I'm sure you use function overloading all the time, without even thinking about it:

    Code:
    std::string str;
    int i;
    double d;
    char c;
    my_special_class myc;
    
    std::cout << str << i << d << c << myc << "\n";
    //here operator << is overloaded for various types to print a variable
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    well then,i suppose it definetly couldnt hurt to learn it.to say the least..and also,why did bs decide to let you change the operators?that seems odd what would be the point of making + equal -

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    That's abuse of operator overloading. It can be very useful when used properly, however. I'm sure you've used the + operator for strings to concatenate strings together, and =, <, and > to compare them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 08:29 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21