C++ function overriding

前端 未结 6 2166
独厮守ぢ
独厮守ぢ 2020-12-30 01:48

I have three different base classes:

class BaseA
{
public:
    virtual int foo() = 0;
};

class BaseB
{
public:
    virtual int foo() { return 42; }
};

clas         


        
相关标签:
6条回答
  • 2020-12-30 02:21

    The important rule to remember is once a function is declared virtual, functions with matching signatures in the derived classes are always virtual. So, it is overridden for Child of A and Child of B, which would behave identically (with the exception of you can't directly instantiate BaseA).

    With C, however, the function isn't overridden, but overloaded. In that situation, only the static type matters: it will call it on what it is a pointer to (the static type) instead of what the object really is (the dynamic type)

    0 讨论(0)
  • 2020-12-30 02:21

    It mostly depends on how you called it.

    if you did:

    class Child : public BaseA
    {
    public:
        int foo() { return 42; }
    };
    

    and did

    BaseA baseA = new Child();
    baseA->foo();
    

    It would call Child's foo function.

    However, if you did this:

    BaseA baseA = new BaseA();
    

    It would produce a compile time error.

    0 讨论(0)
  • 2020-12-30 02:28

    With BaseA, the child class doesn't compile, the pure virtual function isn't defined

    This is true only if you try to create an object of BaseA. If you create a object of Child and then you can call foo() using either BaseA* or Child*

    With BaseB, the function in the child is called when calling foo on a BaseB* or Child*.

    Depends upon the type of the object as the object can be either BaseB or Child. If the object is BaseB then BaseB::foo is called.

    With BaseC, thefunction in the child is called when calling foo on Child* but not on BaseB* (the function in parent class is called).

    Yes, but you never want to do this.

    0 讨论(0)
  • 2020-12-30 02:31

    Class Child will compile if derived from A, you just can't instantiate objects of that type.

    This might be valuable if you were going to override some functions from Base, and then derive again.

    0 讨论(0)
  • 2020-12-30 02:32

    From a polymorphism point of view, prefer A, so you know each child has his own implementation of the virtual function.
    Choose B mainly if you have a valid default implementation, but then you have to make sure that all child-classes have their own implementation as needed. C is not polymorphism, so use judiciously.

    0 讨论(0)
  • 2020-12-30 02:35

    In the derived class a method is virtual if it is defined virtual in the base class, even if the keyword virtual is not used in the derived class's method.

    • With BaseA, it will compile and execute as intended, with foo() being virtual and executing in class Child.
    • Same with BaseB, it will also compile and execute as intended, with foo() being virtual() and executing in class Child.
    • With BaseC however, it will compile and execute, but it will execute the BaseC version if you call it from the context of BaseC, and the Child version if you call with the context of Child.
    0 讨论(0)
提交回复
热议问题