Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Returning a reference to a temporary

Reply
Thread Tools

Returning a reference to a temporary

 
 
marco_segurini
Guest
Posts: n/a
 
      01-10-2005
Hi,
I like to know if the statement at line 15 is correct or not.

#include <vector>

class Descriptor
{
private:
enum { TYPE_0, TYPE_MAX };
Descriptor & Init0()
{
return *this;
}
public:
static void Init(std::vector<Descriptor> & vDescr)
{
vDescr.reserve(TYPE_MAX);
vDescr.push_back(Descriptor().Init0()); // line 15
}
};

TIA.
Marco.
 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      01-10-2005
marco_segurini wrote:
> Descriptor & Init0()
> {
> return *this;
> }
> public:
> static void Init(std::vector<Descriptor> & vDescr)
> {
> vDescr.reserve(TYPE_MAX);
> vDescr.push_back(Descriptor().Init0()); // line 15
> }


This is OK since temporary live until the end of the full
expression. However, you can actually 'push_back()' the
temporary directly without a call ot 'Init0()' as the
generated copy constructor takes a 'const&' parameter anyway.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning a (const) reference to a temporary AdlerSam C++ 10 02-22-2011 01:02 PM
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
returning copy of the temporary string object qazmlp C++ 3 03-07-2004 04:57 PM
Returning a reference to an existing C++ object as a reference JustMe Perl Misc 1 08-29-2003 07:02 AM
iterator / returning reference to local temporary Alexander Stippler C++ 2 07-04-2003 04:40 PM



Advertisments