Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Returning a class instance by value and a naming question

Reply
Thread Tools

Re: Returning a class instance by value and a naming question

 
 
Stefan Ram
Guest
Posts: n/a
 
      11-14-2013
DSF <(E-Mail Removed)> writes:
> I have a class I've been writing where I frequently need to return
>the class by value so that class members can be changed without
>affecting the original.


What you return is called »an instance of the class«
or »an object of the class«. It's not the class.

>upon return, the local instance is copied into an unnamed instance


Return values are rvalues, they are moved, not copied.

> So the question is: Are there any techniques for minimizing the
>number of copies that occur when returning by value?


Move members (move constructors and move assignements) are
already generated by the compiler, unless you defined one of
them or copy members or destructors.

I think, you just have to take care to mark non-obvious
rvalues with ::std::move and to have handles to resources
as members (not large members).

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-15-2013
DSF wrote:
> On 14 Nov 2013 00:31:03 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram)
> wrote:
>>
>> Return values are rvalues, they are moved, not copied.

>
> That's an issue of semantics. A copy followed by a delete is often
> called a move, but there's still a copy involved.
> Return by value:
> Copy local object to unnamed object created before function call.
> Call local object's destructor.
> Return from function.
> Copy unnamed object to target.
> Call unnamed object's destructor.
>
> Call it two "moves," but there's still two copies made.


Are you familiar with the concept of Return Value Optimisation? If not,
look it up. In most situations, what you think may be an expensive
operation, such as returning a container by value, isn't.

--
Ian Collins
 
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
Re: Returning a class instance by value and a naming question Jorgen Grahn C++ 1 11-15-2013 02:58 PM
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments