Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++
|
BOOK: Professional C++
This is the forum to discuss the Wrox book Professional C++ by Nicholas A. Solter, Scott J. Kleper; ISBN: 9780764574849
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2006, 12:41 AM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default copty constructor vs assignment operator

In chapter 8 ,regarding the concept on page 180 181
In case of
string s1,
s1 = myCell2.getString();

the copy constructor is called for creating the temporary obj
and then the assignment operator.

Then in case of

String s2 = myCell3.getString();

2 copy constructors should be called . One for creating temporary object and another for s2.
But copy constructor is getting called only once.
Why is this?




 
Old September 3rd, 2006, 02:10 AM
Authorized User
 
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the first example, s1 is already constructed (on the previous line) so the assignment operator will be used to assign the temporary result object to s1.

In the second example, the compiler recognizes that s2 is just being built, so it uses the copy constructor. The compiler is allowed to omit the temporary return object in this case as a performance enhancement -- there's really no need to call the copy constructor twice when the result value can be copied directly into s2.

Hope that helps!

Scott

----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)





Similar Threads
Thread Thread Starter Forum Replies Last Post
please help me out with this assignment... vaidehi BOOK: Beginning Linux Programming, 4th Edition ISBN 978-0-470-14762-7 3 September 10th, 2008 03:57 PM
assignment help needed rsv C# 3 September 14th, 2007 02:57 PM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
assignment pooja85jain SQL Language 1 September 2nd, 2006 11:09 AM
Programming assignment. Neeko C++ Programming 16 December 18th, 2005 09:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.