C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: Why no implicit declaration of necessary overrides in final classes?
From: Eyal Rozenberg (eyalroz_at_[hidden])
Date: 2020-08-15 06:42:18


I'm not sure why this is a counter-example: the implicit declaration
would only be for the _missing_ methods - those which must be declared
for C to be used.

On 15/08/2020 13:00, Andrey Semashev via Std-Discussion wrote:
> On 2020-08-15 12:01, Eyal Rozenberg via Std-Discussion wrote:
>> Consider the following code:
>> ```
>> class A {
>>      virtual int foo() = 0;
>> };
>>
>> class B final : A {
>>      int foo() override; // (*)
>> };
>>
>> int B::foo() { return 123; }
>> ```
>> ... which fails to compile if we remove the declaration (*).
>>
>> Since `B` is a final class, it must necessarily have all methods defined
>> immediately or by inheritance. Specifically, we must define all `B`
>> methods which are not defined for its (single) base class `A`.
>>
>> Why, then, must `B` declare `foo()` explicitly? Why is the implicit need
>> for the definition not sufficient to allow this code to compile? I mean,
>> I assume the standard requires an explicit declaration here, but what is
>> the rationale for this requirement?
>>
>> An obvious counter-rationale is DRY: If class A has N virtual methods
>> and M final subclasses, we'll have to write M*N redundant declarations.
>> And these redundant declarations also visually "mask", to some extent,
>> the declarations of those methods which are specific to B.
>
> A counter-example:
>
>   class A {
>        virtual int foo() = 0;
>   };
>
>   class B : A {
>        int foo() override;
>   };
>
>   class C final : B {
>   };
>
>   C* p = new C();
>   p->foo();
>
> If the declaration of C::foo was implicit, the compiler would have
> mistakenly generated a call to C::foo even if it doesn't exist.
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.isocpp.org%2Fmailman%2Flistinfo.cgi%2Fstd-discussion&data=02%7C01%7Ceyalroz%40ef.technion.ac.il%7C0aedfc046752400f572808d841022234%7Cf1502c4cee2e411c9715c855f6753b84%7C1%7C0%7C637330824729372154&sdata=amThouEQNhfm75XmEgTotS7SEpnGMKmZoaBI2NalGpU%3D&reserved=0
>
> This email is from an external mail server, be judicious when opening
> attachments or links


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

Older Archives on Google Groups