CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2007
    Posts
    53

    Overload vs Override

    Very Tricky and confusing...
    Can someone clearly distinguish between the two ?

    Thanks

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Overload vs Override

    It's actually simple....these are overloads....


    void func( int );
    void func( double );
    void fund( unsigned int );


    They are the same function name, but are 'different' functions based on the parameters accepted.

    You can't differentiate overloads if the only difference is the return type.

    Key point - you can 'select' among multiple functions of the same name based on the type you pass as a parameter, they need not be member functions, but certainly can be member functions.


    Override....

    Code:
    class A
    {
     public:
    
     void func( int );
    };
    
    
    class B : public A
    {
     public:
    
     void func( int );
    };

    The func in class B overrides the func of class A.

    If you have a instantiation of B, and call func, the one in B will be executed, not the one in A.

    If, on the other hand, you have a B, but you refer to it using a pointer to type A, the func of A would be executed.

    If you declared the functions to be virtual IN A - then if you called func using a pointer to type A, but the object was actually created as type B, THEN the func in B would be called - by virtual routing.

  3. #3
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Overload vs Override

    You overload by adding multiple method definitions (same name, but different argument lists). You override by changing the definition of a base class' method (polymorphism).

    - petter

  4. #4
    Join Date
    Jan 2007
    Posts
    53

    Re: Overload vs Override

    Thnaks to both of you guys
    cheers

  5. #5
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Overload vs Override

    Hi all.

    Quote Originally Posted by JVene
    It's actually simple....these are overloads....
    Code:
    class A
    {
    public:
      void func( int );
    };
    
    class B : public A
    {
    public:
      void func( int );
    };
    Based on the code above, it's also worth to make two observations:

    - Usually, if you want to provide dynamic polymorphism, you should declare your function with the "virtual" modifier.

    - Sometimes, you want to overload a member function in a class hierarchy. In this case, because classes are namespaces in C++, you need to declare that you're "using" a base class function. For example, in the code below, if you don't tell that you're using the function from class A, the code will not compile (at least if you're using a standardized compiler).

    Code:
    class A
    {
    public:
      void func(int);
    };
    
    class B : public A
    {
    public:
      using A::func;
      void func(int, int);
    };
    
    int main(int argc, char* argv)
    { 
      B b;
      b.func(1);
      return 0;
    }

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Overload vs Override

    We had this discussion on here some while back...

    We eventually concluded that the standard's use of "override" specifically refers to virtual functions. JVene's example is not an override - Scott Meyers refers to it as a redefinition. In the absence of a specific term in the standard, we felt that "redefinition" was as good a word as any.

    One point to note - overrides are a Good Thing, redefinitions are a Bad Thing.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Overload vs Override

    Quote Originally Posted by JVene
    It's actually simple....these are overloads....

    You can't differentiate overloads if the only difference is the return type.
    What's the meaning of the above line.As it is already known to all of us that Function Overloading Doesn't Depend on the return type.So may be you can get a error too.
    Thanx

  8. #8
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Overload vs Override

    We eventually concluded that the standard's use of "override" specifically refers to virtual functions. JVene's example is not an override - Scott Meyers refers to it as a redefinition.
    I've not read Scott Meyers. There were called overrides in the general text available back in the late 80's and early 90's, but I've not bothered to check what the lingo has evolved to these days.

    Indeed - they can be confusing unless they are virtual functions.


    Of my quote

    You can't differentiate overloads if the only difference is the return type.

    If the function signature of two proposed overloads are identical except for the return type, the compiler generates and error.



    void func( int );
    bool func( int );

    The second one will generate an error.

  9. #9
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Overload vs Override

    Quote Originally Posted by JVene
    I've not read Scott Meyers. There were called overrides in the general text available back in the late 80's and early 90's, but I've not bothered to check what the lingo has evolved to these days.
    This is also clear from the language standard.

    "hiding" and overriding are two very different things. It applies to all members: static or non-static, data or function.
    Function/variable hiding is a simple consequence of rules concerning class scopes.

    Similarly, there is a third "thing" that I wouldn't call overriding but may look like overriding for an inexperienced programmer who doesn't master the language:
    Code:
    class X {
      public:
      void func();
    };
    class Y:public X {
    #define func other_func /* This is neither overriding, nor scope hiding, it's token replacement */
      void other_func();
    };
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Overload vs Override

    Quote Originally Posted by SuperKoko
    Function/variable hiding is a simple consequence of rules concerning class scopes.
    I would have said that it's a simple consquence of the rules concerning name lookup, but they amount to pretty much the same thing...
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured