dude u haven't used copy constructor properly .. check it out again ... you are not copying actually ... thats it ...
On Sun, Aug 21, 2011 at 12:53 PM, Abhishek Yadav <[email protected] > wrote: > Check this code: the thing i couldn't understand is when the object is > being returned neither the copy constructor is being called nor the > assignment operator overload is called....then how the object is being > copied into b. i don't think default copy constructor should be called if i > have made our own copy constructor....??? > #include<iostream> > using namespace std; > #include<conio.h> > > class X > { > public: > int num; > X(int a) > { > num=a; > cout<<"\n constructor"; > } > > X(const X& t) > { > this->num=t.num; > cout<<"\nCopy "; > } > > X operator=(const X& t) > { > this->num =t.num; > cout<<"\n Assigment "; > return t; > } > > X fun() > { > cout<<"\nin fun"; > return X(7); > } > > ~X() > { > cout<<"\ndestruct "; > } > > }; > > int main() > { > { > X a(1); > X b=a.fun(); > cout<<"\n\n"<<b.num; > } > getch(); > } > > > On Sun, Aug 21, 2011 at 12:33 PM, Abhishek Yadav < > [email protected]> wrote: > >> The code is correct..return X will make a temporary object and for that a >> constructor and corresponding destructor will be called and that object is >> returned. >> >> On Sun, Aug 21, 2011 at 12:24 PM, Puneet Chawla < >> [email protected]> wrote: >> >>> It will show error >>> >>> On Sun, Aug 21, 2011 at 12:21 PM, Sanjay Rajpal <[email protected]>wrote: >>> >>>> I think it will not be an error. >>>> >>>> This is because X() will create a temporary object, and when the >>>> object is returned in the function calling it, then default copy >>>> constructor will do bitwise copy of data members in the calling >>>> function. >>>> >>>> Correct me if m wrong. >>>> >>>> On 8/20/11, sachin sabbarwal <[email protected]> wrote: >>>> > class X() >>>> > { >>>> > >>>> > X() >>>> > { >>>> > } >>>> > >>>> > >>>> > >>>> > X fun() >>>> > { >>>> > return X(); //error or what?? because constructor never returns >>>> > anything so what this return statement will receive after executing >>>> x() and >>>> > what it will return?? >>>> > } >>>> > >>>> > >>>> > }; >>>> > >>>> > -- >>>> > You received this message because you are subscribed to the Google >>>> Groups >>>> > "Algorithm Geeks" group. >>>> > To post to this group, send email to [email protected]. >>>> > To unsubscribe from this group, send email to >>>> > [email protected]. >>>> > For more options, visit this group at >>>> > http://groups.google.com/group/algogeeks?hl=en. >>>> > >>>> > >>>> >>>> >>>> -- >>>> Sanju >>>> :) >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Algorithm Geeks" group. >>>> To post to this group, send email to [email protected]. >>>> To unsubscribe from this group, send email to >>>> [email protected]. >>>> For more options, visit this group at >>>> http://groups.google.com/group/algogeeks?hl=en. >>>> >>>> >>> >>> >>> -- >>> With regards >>> ............ >>> Puneet Chawla >>> Computer Engineering Student >>> NIT Kurukshetra >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- JaiDev Yadav (National Yoga Champion) Computer Engg. Dept. National Institute of Technology Kurukshetra,Haryana -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
