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.

Reply via email to