class A{ // .... }; class B{ public: explicit B(const A& a) : i_a(a) { } ~B() { } private: const A& i_a; };
A returnA( const char* arg ) { return A(/*arg*/); } foo() { const A& aRef = returnA("FirstObject"); // ..... (1) { const B b(returnA("SecondObject")); // ..... (2) ///... typedef int outofscope_block; // ..... (3) } typedef char outofscope_function; // ..... (4) } temporary object created and referenced by aRef is being deleted when aRef goes out of scope in foo, as Expected. temporary object created and referenced by B::i_a is being deleted right after object b's construction, Not expected. It should(?) have the same life time as the B::i_a(thus b), such as deleted _after_ line (3) --- If this assumpsion is false, then what's the difference between a 'plain reference' and 'member reference' herein?? BTW, it works as expected on SunCC compiler. It core dumps on gcc 3.3.3 while having (empty/invalid) data for i_a on gcc 4.1.2 but not coring. -- Summary: member reference to a temporary object being deleted too eary Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: gzljg at hotmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33885