C++ Logo

STD-DISCUSSION

Advanced search

Subject: Maybe a wording defect for virtual function in the standard
From: jim x (xmh970252187_at_[hidden])
Date: 2020-09-21 22:29:31


Hi, guys.

After reading the [class.virtual#2]
<https://timsong-cpp.github.io/cppwp/n4659/class.virtual#2> section, I
found a wording defect in this section, that is:

For convenience we say that any virtual function overrides itself. A
virtual member function C::vf of *a class object S* is a final overrider
unless *the most derived class of which S is a base class subobject* (if
any) declares or *inherits another member function that overrides vf*.

consider the below code:
````
#include <iostream>
struct A {
    virtual void f(){
        std::cout<<"1\n";
    }
};
struct B : A {

};
struct C : A {
    void f(){
        std::cout<<"abc\n";
    }
};
struct D : B, C {

};
int main(){
  D mostDerived{};
  D* ptr = &mostDerived;
  B* bptr = ptr;
  A* aptr = bptr;
  aptr->f();
}
````
I would say the outcome should be `abc` rather than `1`, my reason is,
according to the above rules:
consider the base class subobject of type A in B and in C as A1, A2
respectively.
In my example, the most derived class is D, According to the above rules,
for class B, the final overrider is A::f which is inherits from A, for
class C, the final overrider is C::f which is declared in class C, and for
class D, it's derived from B and C, so it will inherit these members from B
 and C. Since C::f overrides A::f, So according to the rule unless **the
most derived class of which S is a base class subobject** (if any) declares
or **inherits another member function that overrides vf**, where consider
the A1 as S(namely, A1 is the subobject of class D), where D inherits
the another
member function that overrides vf which is C::f. that means, the final
override for the subobject A1 should be C::f, hence , I wonder why the
outcome wasn't abc instead for 1?

I also post this question on SO
<https://stackoverflow.com/questions/63992216/an-issue-about-the-description-of-virtual-function-in-the-standard>,
the answers agree this is a defective.

Maybe, the above rule could be changed to "For convenience we say that any
virtual function overrides itself. A virtual member function C::vf of a
class object S is a final overrider unless the most derived class of which
S is a base class subobject (if any) declares that function or inherits
another member function that overrides `S's` vf, the member function was
declared within the class of a subobject of which S is its subobject " , if
`S` here refers to the class in which the function was first declared as
virtual.

Or a better way to describe this procedure.



STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups