This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++/5607: No pointer adjustment in covariant return types



>Number:         5607
>Category:       c++
>Synopsis:       No pointer adjustment in covariant return types
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Feb 06 02:56:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Alex Rozenman
>Release:        gcc 3.0
>Organization:
>Environment:
Solaris 2.6
>Description:
When you run the attached program you can see that
pointer to B instance before and after covariant return
are different.

This is copy of my test case (the same as the attached file):

#begin

/**
 * This is test case for covariant pointers 
 * compiler implementation.
 * Numbers in each result line should be
 * the same.
 */

#include <iostream.h>

class A {
public:
  virtual A* getThis() { return this; }
};

class B {
int a;
public:
  virtual B* getThis() { return this; }
};

class AB : public A, public B {
public:
  virtual AB* getThis() { return this; }
};


int main () {

  using namespace std;

  AB* ab = new AB();
  
  A* a = ab;
  B* b = ab;

  cout << a << " " << a->getThis() <<  endl;
  cout << b << " " << b->getThis() <<  endl;

  return 0;
}

#end
>How-To-Repeat:
Compile the test case, run it. 
In the second line you can see two different 
pointers to the same instance ('B' in the program)
>Fix:
Adjust the pointer appropriately
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="Covariant.cpp"
Content-Disposition: inline; filename="Covariant.cpp"


/**
 * This is test case for covariant pointers 
 * compiler implementation.
 * Numbers in each result line should be
 * equal. 
 */

#include <iostream.h>

class A {
public:
  virtual A* getThis() { return this; }
};

class B {
int a;
public:
  virtual B* getThis() { return this; }
};

class AB : public A, public B {
public:
  virtual AB* getThis() { return this; }
};


int main () {

  using namespace std;

  AB* ab = new AB();
  
  A* a = ab;
  B* b = ab;

  cout << a << " " << a->getThis() <<  endl;
  cout << b << " " << b->getThis() <<  endl;

  return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]