*Case 1:*
x g()
{
 x b; *// Constructor*
 b.print();
 return b; *// Optimized away*
}
int main()
{
 x b; *// Constructor*
 x t=g(); *// Optimized away*
}

*Case 2:*
x g(x b) *// Copy constructor*
{

 b.print();
 return b; *// Return by value & destructor*
}
int main()
{
 x b; *// Constructor*
 x t=g(b); *// Pass by value and return by value*
}

This has to do with optimization of local objects done by the compiler.
Since in case 1 a local was to created/returned to initialize another local,
so what the compiler does is that it creates object 'b' at a memory location
where object t was to be created. (You may think of the compiler internally
using placement new)

Different compilers have different techniques of optimization.
Refer http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.10
And as per the above link this should not work on MS C++.NET



Regards,
Sandeep Jain



On Wed, Jul 13, 2011 at 10:35 AM, pawan yadav <pawan1991ya...@gmail.com>wrote:

> @rahul thanks for reply
> o/p (of 1st program) :
> constructor
> constructor
> print statement
>  destructor
>  destructor
>
>
> here copy constructor is not called ... that's why i have posted this
> program
>
> On Tue, Jul 12, 2011 at 10:42 PM, rahul <rahulr...@gmail.com> wrote:
>
>> Hi,
>>
>> In first program,it will call copy constructor once, while returning
>> object by value.
>> In 2nd program it will call copy constructor twice, once when u pass an
>> object to function by value, and 2nd time when you return an object by value
>> from function.
>>
>> Thanks.
>>
>> On Tue, Jul 12, 2011 at 11:52 PM, segfault <pawan1991ya...@gmail.com>wrote:
>>
>>> #include<iostream>
>>> using namespace std;
>>> class x{
>>>  int p;
>>>  public:
>>>  x(){cout<<"constructor\n";}
>>>  x(const x &y)
>>>  {
>>>  cout<<"copy constructor\n";
>>>  }
>>>  ~x()
>>>  {
>>>  cout<<" destructor\n";
>>>  }
>>>  void print()
>>>  {
>>>  cout<<"print statement\n";
>>>  }
>>> };
>>> x g()
>>> {
>>>  x b;
>>>  b.print();
>>>  return b;
>>> }
>>> int main()
>>> {
>>>  x b;
>>>  x t=g();
>>> }
>>>
>>>
>>>
>>> #include<iostream>
>>> using namespace std;
>>> class x{
>>>  int p;
>>>  public:
>>>  x(){cout<<"constructor\n";}
>>>  x(const x &y)
>>>  {
>>>  cout<<"copy constructor\n";
>>>  }
>>>  ~x()
>>>  {
>>>  cout<<" destructor\n";
>>>  }
>>>  void print()
>>>  {
>>>  cout<<"print statement\n";
>>>  }
>>> };
>>> x g(x b)
>>> {
>>>
>>>  b.print();
>>>  return b;
>>> }
>>> int main()
>>> {
>>>  x b;
>>>  x t=g(b);
>>> }
>>>
>>>
>>> why first one is not calling copy constructor in function g() while
>>> returning from it
>>> but second one is  calling copy constructor in function g() while
>>> returning from it?
>>>
>>> in both program inside g() b is local but why giving different result.
>>>
>>> bruce ackel page number:467
>>>
>>>
>>>
>>> please explain it.
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to