Reference to pointer and cast - is this a bug ?

Philippe Combes Philippe.Combes@ens-lyon.fr
Fri Dec 1 09:15:00 GMT 2006


Hi all,

Here is code that works exactly as I think it should with g++-2.95, but
not with
g++-4.0/4.1

#include <iostream>
using namespace std;

class parent
{
public:
         parent():i(0){}
         parent(int i_):i(i_){}
private:
         int i;
};

class child : public parent
{
public:
         child():parent() {}
         child(int i_):parent(i_){}
};

void trace( const parent* const& p_ptr )
{
         cout << "trace called on a reference tp pointer @"
              << (void*)&p_ptr << ": pointer = " << (void*)p_ptr << endl;
}

int main( int argc, char** argv )
{
         const child c(4);
         const child* c_ptr = &c;
         const child* const& c_ptr_r = c_ptr;
         cout << "Call trace on a reference to pointer @"
              << (void*)&c_ptr_r << ": pointer = " << (void*)c_ptr_r <<
endl;
         trace( c_ptr_r );
         return 0;
}


Output with g++-2.95 (which I think is right)
Call trace on a reference to pointer @0xbfb43a40: pointer = 0xbfb43a44
trace called on a reference tp pointer @0xbfb43a40: pointer = 0xbfb43a44

Output with g++-4.0 or 4.1
Call trace on a reference to pointer @0xbfac899c: pointer = 0xbfac89a0
trace called on a reference tp pointer @0xbfac89a4: pointer = 0xbfac89a0

which means that the pointer passed by reference to trace has been
copied, just
as if it was passed by value.

If this is a bug, I did not find it in the bug database.
If not, can anybody tell me why ?


Thanks in advance,

Philippe





More information about the Gcc-help mailing list