overloading vs overriding

后端 未结 4 1584
春和景丽
春和景丽 2021-01-02 02:00

I am a little confused over the two terminologies and would be glad to get some doubts clarified.

As I understand function overloading means having mult

相关标签:
4条回答
  • 2021-01-02 02:18

    In this case neither. The derived-class method hides the base-class method.

    0 讨论(0)
  • 2021-01-02 02:20

    Function overloading is when you have several functions which differ in their parameter list or, if they are member functions, in their const/volatile qualification. (In some other languages you can also overload based on the return type, but C++ doesn't allow that.)
    Examples:

    void f(int);
    void f(char);
    
    class some_class {
      void g();
      void g() const;
    };
    

    Function overriding is when you redefine a base class function with the same signature. Usually this only makes sense if the base class function is virtual, because otherwise the function to be called (base or derived class' version) is determined at compile-time using a reference's/pointer's static type.
    Examples:

    class base {
      void f();
      virtual void g();
    };
    
    class derived : public base {
      void f();
      void g();
    };
    

    Function hiding is when you define a function ina derived class (or an inner scope) that has a different parameter list than a function with the same name declared in a base class (or outer scope). In this case the derived class' function(s) hides the base class function(s). You can avoid that by explicitly bringing the base class function(s) into the derived class' scope with a using declaration.
    Examples:

    class base {
      void f(int);
      void f(char);
    };
    
    class derived1 : public base {
      void f(double);
    };
    
    void f()
    {
      derived1 d;
      d.f(42); // calls derived1::f(double)!
    }
    
    class derived2 : public base {
      using base::f; // bring base class versions into derived2's scope
      void f(double);
    };
    
    void g()
    {
      derived2 d;
      d.f(42); // calls base::f(int)!
    }
    

    Just in case it's unclear: Based on these definitions, I'd call the scenario in question here overriding.

    0 讨论(0)
  • 2021-01-02 02:20

    Overloading is the process of defining multiple methods with identical names but different signatures; Overriding is when a function in a child class has an identical signature to a virtual function in a parent class.

    class Test {
      // Test::func is overloaded
      virtual void func(int x);
      virtual void func(double y);
    };
    
    class Child : public Test {
      // Child::func overrides Test::func
      virtual void func(int x); 
    };
    
    0 讨论(0)
  • 2021-01-02 02:24
    • hiding is when a definition in a scope is not accessible due to a declaration in a nested scope or a derived class (3.3.7/1).

    A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class.

    • overriding is when a virtual member is replaced in a derived class (see 10.3/2)

    If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual an it overrides Base::vf.

    • overloading is when several declarations coexist for the same name in the same scope (13/1)

    When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded.

    • related, there is also the possibility of replacing operator new and delete from the standard library by one's own implementation (18.4.1.1/2, 18.4.1.1/6, 18.4.1.1/11, 18.4.1.2)

    So this is clearly a case of hiding.

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