Thread: function overloading, why is it done?

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    3

    function overloading, why is it done?

    Code:
        
     public SqlCommand GetCommand(string commandText, CommandType commandType, params SqlParameter[] parameters) {            SqlCommand cmd = new SqlCommand();
                cmd.Connection = GetConnection();
                cmd.CommandText = commandText;
                cmd.CommandType = commandType;
                foreach(SqlParameter p in parameters) {
                    cmd.Parameters.Add(p);
                }
                return cmd;
            }
    
    
            public SqlCommand GetCommand(string commandText, params SqlParameter[] parameters) {
                return GetCommand(commandText, CommandType.StoredProcedure, parameters);
            }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What? More words...
    If you understand what you're doing, you're not learning anything.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Function overloading can be used when the same function is needed with the same name but with different return types or parameter types.
    When this need occurs is up to the programmer - and can generally be avoided by defining a separate function which achieves the same result as the original function.

    Function is not however the same as operating overloading - which is a different beast entirely.
    Double Helix STL

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Quote Originally Posted by swgh View Post
    Function overloading can be used when the same function is needed with the same name but with different return types or parameter types.
    When this need occurs is up to the programmer - and can generally be avoided by defining a separate function which achieves the same result as the original function.

    Function is not however the same as operating overloading - which is a different beast entirely.
    Not quite, at least not in C based languages. You can't overload by return type, only by function signatures which does not include the return type.
    It can be simulated sort of in both C and C++. It probably can in C# too. The question is why would you want to. That's probably indicative of a design fault.
    Can you think of a groovy way to do this?
    Code:
    ReturnType FuncThatCanReturnIntDoubleOrString(void);
    There's at least one way in C and 3 ways in C++ this can be accomplished. Can't say for C#. I'm a novice in .net world, never used it.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Hobbit View Post
    Can you think of a groovy way to do this?
    Code:
    ReturnType FuncThatCanReturnIntDoubleOrString(void);
    There's at least one way in C and 3 ways in C++ this can be accomplished. Can't say for C#. I'm a novice in .net world, never used it.
    The "object" type can contain any reference or value type so...
    Code:
    object FuncThatCanReturnIntDoubleOrString() { }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Sure that'll work for C# I guess. I don't use C# and .net.

    C doesn't have overloading at all, and C++ only allows overloading by parameter types and cv qualifiers. swgh said you could overload by return type, but that is not allowed. I said it can be simulated.

    How would you simulate overloading by return value in C and C++?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by Hobbit View Post
    How would you simulate overloading by return value in C and C++?
    Use an output parameter? Use a special parameter to force the overload resolution? Use templates? Use implicit conversions via a wrapper class? Use a variant typed structure? Simulate your own "object" class?

    The options, there are many. The big question is what makes the most sense for your program.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function overloading
    By bleuz in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2010, 06:36 PM
  2. Function Overloading
    By vb.bajpai in forum C++ Programming
    Replies: 8
    Last Post: 06-21-2007, 01:29 PM
  3. function overloading
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2006, 12:45 PM
  4. function overloading
    By lambs4 in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2003, 07:03 PM
  5. A little on Function Overloading
    By zbap in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2002, 02:22 PM

Tags for this Thread