Override and overload in C++

后端 未结 7 1487
生来不讨喜
生来不讨喜 2020-12-04 05:56

Yes, I do understand the difference between them. What I want to know is: why OVERRIDE a method? What is the good in doing it? In case of overload: the only advantage is you

相关标签:
7条回答
  • 2020-12-04 06:06

    The textbook example is class Animal with method speak(). The Dog subclass overrides speak() to "bark" while the Cat subclass overrides speak() to "meow".

    0 讨论(0)
  • 2020-12-04 06:08

    Overloading generally means that you have two or more functions in the same scope having the same name. The function that better matches the arguments when a call is made wins and is called. Important to note, as opposed to calling a virtual function, is that the function that's called is selected at compile time. It all depends on the static type of the argument. If you have an overload for B and one for D, and the argument is a reference to B, but it really points to a D object, then the overload for B is chosen in C++. That's called static dispatch as opposed to dynamic dispatch. You overload if you want to do the same as another function having the same name, but you want to do that for another argument type. Example:

    void print(Foo const& f) {
        // print a foo
    }
    
    void print(Bar const& bar) {
        // print a bar
    }
    

    they both print their argument, so they are overloaded. But the first prints a foo, and the second prints a bar. If you have two functions that do different things, it's considered bad style when they have the same name, because that can lead to confusion about what will happen actually when calling the functions. Another usecase for overloading is when you have additional parameters for functions, but they just forward control to other functions:

    void print(Foo & f, PrintAttributes b) { 
        /* ... */ 
    }
    
    void print(Foo & f, std::string const& header, bool printBold) {
        print(f, PrintAttributes(header, printBold));
    }
    

    That can be convenient for the caller, if the options that the overloads take are often used.

    Overriding is something completely different. It doesn't compete with overloading. It means that if you have a virtual function in a base class, you can write a function with the same signature in the derived class. The function in the derived class overrides the function of the base. Sample:

    struct base {
        virtual void print() { cout << "base!"; }
    }
    
    struct derived: base {
        virtual void print() { cout << "derived!"; }
    }
    

    Now, if you have an object and call the print member function, the print function of the derived is always called, because it overrides the one of the base. If the function print wasn't virtual, then the function in the derived wouldn't override the base function, but would merely hide it. Overriding can be useful if you have a function that accepts a base class, and every one that's derived from it:

    void doit(base &b) {
        // and sometimes, we want to print it
        b.print();
    }
    

    Now, even though at compile time the compiler only knows that b is at least base, print of the derived class will be called. That's the point of virtual functions. Without them, the print function of the base would be called, and the one in the derived class wouldn't override it.

    0 讨论(0)
  • 2020-12-04 06:17

    Override is useful when you inherit from a base class and wish to extend or modify its functionality. Even when the object is cast as the base class, it calls your overridden function, not the base one.

    Overloading is not necessary, but it sure makes life easier or more readable sometimes. Arguably it can make it worse, but that's when it should not be used. For example, you can have two functions that perform the same operation, but act on different kinds of things. For example Divide(float, float) should be different from Divide(int, int), but they're basically the same operation. Wouldn't you rather remember one method name, "Divide", than have to remember "DivideFloat", "DivideInt", "DivideIntByFloat", and so on?

    0 讨论(0)
  • 2020-12-04 06:19

    This will add some more clarity to thoughts.enter image description here

    0 讨论(0)
  • 2020-12-04 06:24

    One use of overloading is for use in templates. In templates, you write code that can be used on different data types, and call it with different types. If functions that take different arguments had to be named differently, the code for different data types would in general have to be different, and templates just wouldn't work.

    While you may not be writing templates yet, you're almost certainly using some of them. Streams are templates, and so are vectors. Without overloading, and therefore without templates, you'd need to call Unicode streams something different from ASCII streams, and you'd have to use arrays and pointers instead of vectors.

    0 讨论(0)
  • You overload functions for three reasons:

    1. To provide two (or more) functions that perform similar, closely related things, differentiated by the types and/or number of arguments it accepts. Contrived example:

      void Log(std::string msg); // logs a message to standard out
      void Log(std::string msg, std::ofstream); // logs a message to a file
      
    2. To provide two (or more) ways to perform the same action. Contrived example:

      void Plot(Point pt); // plots a point at (pt.x, pt.y)
      void Plot(int x, int y); // plots a point at (x, y)
      
    3. To provide the ability to perform an equivalent action given two (or more) different input types. Contrived example:

      wchar_t      ToUnicode(char c);
      std::wstring ToUnicode(std::string s);
      

    In some cases it's worth arguing that a function of a different name is a better choice than an overloaded function. In the case of constructors, overloading is the only choice.


    Overriding a function is entirely different, and serves an entirely different purpose. Function overriding is how polymorphism works in C++. You override a function to change the behavior of that function in a derived class. In this way, a base class provides interface, and the derived class provides implementation.

    0 讨论(0)
提交回复
热议问题