From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32543 invoked by alias); 1 Dec 2006 09:15:48 -0000 Received: (qmail 32535 invoked by uid 22791); 1 Dec 2006 09:15:47 -0000 X-Spam-Check-By: sourceware.org Received: from smtp4-g19.free.fr (HELO smtp4-g19.free.fr) (212.27.42.30) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 01 Dec 2006 09:15:42 +0000 Received: from [192.168.1.1] (lns-bzn-27-82-248-17-99.adsl.proxad.net [82.248.17.99]) by smtp4-g19.free.fr (Postfix) with ESMTP id 5C08B89B2 for ; Fri, 1 Dec 2006 10:15:39 +0100 (CET) Message-ID: <456FF2CE.7090304@ens-lyon.fr> Date: Fri, 01 Dec 2006 09:15:00 -0000 From: Philippe Combes User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.7.13) Gecko/20060809 Debian/1.7.13-0.3 MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: Reference to pointer and cast - is this a bug ? Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2006-12/txt/msg00003.txt.bz2 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 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