Compiling and executing this code:
---------------------------------
#include <iostream>
using namespace std;

void *p;

struct vbase
{
  int a;
  virtual vbase* me() = 0;
  void set_p() { p = me(); };
};

struct derived : virtual vbase
{
  int b;
  derived* me()
  {
    cout << "derived::me() = " << this << endl;
    return this;
  };
};

int main()
{
  derived t;
  t.set_p();
  cout << "p = " << p << endl;
};
---------------------------------

produce the strange results, the output is:
---------------------------------
derived::me() = 0xbff41ab0
p = 0xbff41ab8
---------------------------------

It doesn't metter is me() a pure virtual function or has a body in 'vbase'
class.

Small changes in code, any of these:
1) declaring the return type of me() as void*
2) commenting data member 'int a' in 'vbase' class
3) making 'vbase' non virtual base of 'derived'
give the right result: saved 'this' in 'p' is equal to 'this' of derived class.

Sorry, don't know how to summarize the problem, is it a bug or I misunderstood
some c++ subtle points...


-- 
           Summary: a bug with virtual base class (?)
           Product: gcc
           Version: 4.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mikeus at hotmail dot ru
  GCC host triplet: i686-pc-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44733

Reply via email to