473,394 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Why is copy constructor called only twice here (should be thrice?)

I am compiling like

g++ -O0 MyString.cpp

Should the copy cnstrs be called thrice? Any optimization going on
here?

--------------------------
#include <iostream>
#include <string>
using namespace std;
class MyString: public string {
public:
MyString(const char * str): string(str) {
cout << "copy ctr 1 called" << endl;
}
MyString(MyString const & str): string(str) {
cout << "copy ctr 2 called" << endl;
}
};

MyString getString() {
MyString x("rrr"); //copy ctr 1 called

//This should call copy ctr once to create the temporary
//that will be returned
return x;
}

int main() {
//This should call copy ctr again with the temporary
//created at the end of getString() to create y
MyString y (getString());
}

------------
This program is printing:

copy ctr 1 called
copy ctr 2 called

Jul 23 '05 #1
5 1340

"TechCrazy" <Te*******@hotpop.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I am compiling like

g++ -O0 MyString.cpp

Should the copy cnstrs be called thrice? Any optimization going on
here?


The getString() function is eligible for Named Return Value (NRV)
optimization, which eliminates the temporary.

Regards,
Ben
Jul 23 '05 #2
But, I turned off all optimization with -O0

Jul 23 '05 #3
Ian
TechCrazy wrote:
I am compiling like

g++ -O0 MyString.cpp

Should the copy cnstrs be called thrice? Any optimization going on
here?
Yes, the object returned by getString is y.
--------------------------
#include <iostream>
#include <string>
using namespace std;
class MyString: public string {
public:
MyString(const char * str): string(str) {
cout << "copy ctr 1 called" << endl;
}

Not a copy constructor.

Ian
Jul 23 '05 #4
Ian, I think the OP wants to ask why the constructor was called twice than
three times.

ben
Jul 23 '05 #5
TechCrazy wrote:

But, I turned off all optimization with -O0


Then ask the guys writing your compiler, why NRVO is still
done in that case.

I guess the answer will be something like: NRVO is always on, because
it is an optimization which affects the callee as well as the caller.
If we would allow to disable it, then there could be a situation, where
the caller is compiled for NRVO, while the callee is not. And that will
give you a big crash.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gandalf | last post by:
Hello. If I have classes that allocates dynamic memory, it is a good thing to have a copy constructor(CC). If I have a CC, now I'm only trying to understand when I also need to overload the...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
1
by: Vivek Shah | last post by:
Hi, Given below is apiece of code which I was writing to clear my concepts of copy constructor. I have a function f() which takes Class A object through call by value and return the same object....
7
by: Kelly Mandrake | last post by:
I've implemented a class with operator+ overloaded and I also provided my own copy constructor; The problem is my copy constructor is being called twise and I dont understand why. I believe the...
3
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
20
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object...
6
by: suresh | last post by:
Hi Could you please tell why copy constructor is not called in the first line in main(), as mentioned in the text books. I used g++ version 4.1.2 on debian etch thanks suresh # include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.