https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106924
Bug ID: 106924 Summary: Nested class: virtual function returns wrong pointer of covariant type Product: gcc Version: 10.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: aeiken at motortech dot de Target Milestone: --- Created attachment 53570 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53570&action=edit test.ii file generated with g++ -save-temps test.cpp The following was found with gcc-arm-none-eabi-10.3-2021.07, tests with other versions for linux (Ubuntu 20.04.3 LTS) did also not work (e.g. g++ 9.4.0 on Ubuntu). Take a look at the example code in test.cpp: A class (ABFabNested) is nested inside a class (class DerivedAB with multiple inheritances (class BaseA and BaseB)). Because of the class layout, the adresses of DerivedAB and BaseB are different. The nested class itself is derived from a Base class (FabForB) which has a virtual function(getPtr), which return a pointer to class BaseA. The nested class overrides this virtual function, but returns a pointer to class DerivedAB. This is allowed, because DerivedAB and BaseB are covariants. If you have an instance of a pointer to FabForB, which points to an instance of ABFabNested, getPtr returns the address of DerivedAB, not BaseA. If you implement the same class (here ABFab) outside of DerivedAB (not nested) and repeat the step before, getPtr returns the address of BaseA, which is what I expected. test.cpp was compiled with the 9.4.0 version of the g++ without any parameters: g++ a.out Output of a.out: TEST fab: addr e89f6030 base e89f6040 TEST addr e89f6040 TEST fab nested: addr e89f6010 base e89f6020 TEST nested addr e89f6010 g++ --version g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The test was repeated with other g++ versions (also newer version) with the same result. test.cpp: #include <stdio.h> class BaseA { public: virtual BaseA* getPtr(){return this;} virtual ~BaseA(){} }; class BaseB { public: int i = 0; virtual int geti(){return i;}; virtual ~BaseB(){} }; class FabForB { public: virtual BaseA* getPtr()=0; virtual ~FabForB(){} }; class DerivedAB: public BaseB, public BaseA { public: virtual DerivedAB* getPtr(){printf("TEST D this %x\n", this);return this;} virtual int geti(){return 5;}; class ABFabNested: public FabForB { public: virtual DerivedAB* getPtr() { static DerivedAB staticD; BaseA* basePtr = &staticD; printf("TEST fab nested: addr %x base %x\n", &staticD, basePtr); return &staticD; } virtual int geti(){return 5;}; }; }; class ABFab: public FabForB { public: virtual DerivedAB* getPtr() { static DerivedAB staticD; BaseA* basePtr = &staticD; printf("TEST fab: addr %x base %x\n", &staticD, basePtr); return &staticD; } virtual int geti(){return 5;}; }; int main() { ABFab fab; DerivedAB::ABFabNested fabNested; FabForB* fabPtr = &fab; FabForB* fabNestedPtr = &fabNested; printf("TEST addr %x \n", fabPtr->getPtr()); printf("TEST nested addr %x \n", fabNestedPtr->getPtr()); return 0; }