Thread: overloading

  1. #1
    Registered User
    Join Date
    Nov 2002

    Question overloading

    What is meant by overloading?

    Object a = new Object();
    Object b = a;

    Is this an overloading?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    S/He could be referring to an overloading of the equals operator. However, this is automatically done for you by the compiler if you don't overload it yourself.

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    There is function overloadin, operator overloading, which one do you mean?

  4. #4
    Registered User
    Join Date
    Mar 2002
    operators are shorthand versions for functions, so basically overloading functions and overloading operators amounts to the same thing.

    You overload functions/operators when you want to use the same function with different parameters. perhaps the easiest way is to pretend you are creating a function called add(). To add two ints together you declare this:

    int add(int, int);

    to add a double to an int you do this:

    int add(int, double);

    to add an int to a double you do this:

    int add(double, int);

    to add two doubles you do this:

    int add(double, double);

    Now you need to write the definitions for each function, but, the point is you now have 4 functions all with the name add() and depending on what parameters are passed to add() in your code the compiler will pick out which of the versions of add() is the appropriate one to use. The function add() is said to be overloaded. add() is a sorry excuse for a function, but it get's the point across.

    To become more relevant, lets say you have developed a class to handle fractions and you wish to be able to use the + operator to add two of them together. The compiler knows how to use the + operator when using int, double, or float, but it doesn't know what to do to add two fractions, so you have to tell it what to do by overloading the + operator.

    Another good example would be overloading the constructor of a class, etc. It is a powerful technique once you figure it out.

  5. #5
    Registered User
    Join Date
    Nov 2002
    This looks pretty much like another term overriding. how do i distinguish them?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Overriding is replacing (masking) a function in a class hierarchy with one of the same signature. You can't overload a function appearing in a base class in it's derived class and have the base function called from an instance of the dervived class with the appropriate parameters (without casting).

    Overloading is either providing a different signature for a function or defining a function so that an operator may be used (i.e. not replacing an existing function). Unless the overloaded function prevents one being called with a different signature via standard conversions. In which case you could argue that it is being overridden.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Understanding that the standard library incorporates specific operators to interact with specific "types", the term "overloading" refers to adding additional functionality to those operators.

    In particular, when you define a class, you've created a user-defined "type". Compilers don't recognize the interaction between user-defined "types" and built-in operators. Therefore, you might find it necessary to "overload" an operator, i.e. give it the same functionality with your new type as it would have with built-in types.

    "Overriding" a function, as JoeSixpack points out, "masks" the function in the parent (base) class when you define a function of the same name in a derived class. Since the compiler will look to the "child" before the "parent", the derived function will be executed, rather than the base's function, provided they have the same name.

    "Overriding" is no-brainer. "Overloading" requires that you know which operators "translate", and which do not.

    Pain in the backside as it may be, figure on "overloading" darned-near all operators with your user-defined members.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Overloading operator ==
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 03:26 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading for newbies
    By lime in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 02:58 PM