DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: OutPut of a Program
*****************************************************
#Post#: 24--------------------------------------------------
output...
By: dinesh Date: August 16, 2012, 10:09 am
---------------------------------------------------------
#include <iostream>
using namespace std;
class B
{
public:
B(const char* str = "\0") //default constructor
{
cout << "Constructor called" << endl;
}
B(const B &b) //copy constructor
{
cout << "Copy constructor called" << endl;
}
};
int main()
{
B ob = "copy me";
return 0;
}
#Post#: 27--------------------------------------------------
Re: output...
By: adi Date: August 16, 2012, 11:17 pm
---------------------------------------------------------
Constructor called
#Post#: 28--------------------------------------------------
Re: output...
By: dinesh Date: August 17, 2012, 12:40 am
---------------------------------------------------------
ok, why???
why not the other?
#Post#: 29--------------------------------------------------
Re: output...
By: adi Date: August 17, 2012, 12:49 am
---------------------------------------------------------
There is nothing to copy
#Post#: 59--------------------------------------------------
Re: output...
By: kpr29 Date: August 25, 2012, 5:25 am
---------------------------------------------------------
@Dinesh: I feel the answer is "copy Constructor called". Because
they say the compiler does the optimization. If we doesnt want
the compiler to do optimization then we have give a flag then
both the constructor are called
#Post#: 166--------------------------------------------------
Re: output...
By: satya Date: November 19, 2012, 11:28 pm
---------------------------------------------------------
@dinesh : can u tell the correct answer n y is it so?? ;D
#Post#: 206--------------------------------------------------
Re: output...
By: dinesh Date: November 26, 2012, 9:32 am
---------------------------------------------------------
Copy elision (or Copy omission) is a compiler optimization
technique that avoids unnecessary copying of objects. Now a
days, almost every compiler uses it.
Why copy constructor is not called?
According to theory, when the object “ob” is being constructed,
one argument constructor is used to convert “copy me” to a
temporary object & that temporary object is copied to the object
“ob”. So the statement
B ob = "copy me";
should be broken down by the compiler as
B ob = B("copy me");
However, most of the C++ compilers avoid such overheads of
creating a temporary object & then copying it.
The modern compilers break down the statement
B ob = "copy me"; //copy initialization
as
B ob("copy me"); //direct initialization
and thus eliding call to copy constructor.
However, if we still want to ensure that the compiler doesn’t
elide the call to copy constructor [disable the copy elision],
we can compile the program using “-fno-elide-constructors”
option with g++ and see the output as following:
aashish@aashish-ThinkPad-SL400:~$ g++ copy_elision.cpp
-fno-elide-constructors
aashish@aashish-ThinkPad-SL400:~$ ./a.out
Constructor called
Copy constructor called
If “-fno-elide-constructors” option is used, first default
constructor is called to create a temporary object, then copy
constructor is called to copy the temporary object to ob.
HTML http://en.wikipedia.org/wiki/Copy_elision
#Post#: 212--------------------------------------------------
Re: output...
By: srini Date: November 26, 2012, 11:08 pm
---------------------------------------------------------
@dinesh: nice explanation :o ::)
*****************************************************