GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 144 of 203    Print  
While copying the objects if we say X a=b, X a(b) What will it call, assignment operator or copy constructor? Justify

  
Total Answers and Comments: 4 Last Update: July 28, 2009     Asked by: pradeep kumar 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: lddung19720603
 
In both cases the copy constructor will be called.
If we have a=b alone, then the assignment operator will be called.


Above answer was rated as good by the following members:
es1968
August 11, 2007 21:25:58   #1  
TalasilaNaveen        

RE: While copying the objects if we say X a=b, X a(b) ...

This calls Copy constructor which assigns/copies the data associated with the object b.


 
Is this answer useful? Yes | No
September 15, 2007 19:43:38   #2  
Raj        

RE: While copying the objects if we say X a=b, X a(b) ...


class test
{


public:
int x y;

test ();

test(const test&);

test operator (const test&);

virtual ~test();

};

test::test(const test& b)
{

cout << "i am in a copy constructor"<<endl;

}


test::~test()
{

}


test::test()
{

}

test test::operator (const test& b)
{
if (this &b)
return *this;
cout << "i am using assignment operator "<<endl;

x b.x;
y b.y;

return *this;
}



int main()
{



test d;
test b d;
test e(b);
getchar();


return 0;


}


 
Is this answer useful? Yes | No
February 18, 2009 03:54:27   #3  
santhosh.kanchanapally Member Since: April 2008   Contribution: 10    

RE: While copying the objects if we say X a=b, X a(b) What will it call, assignment operator or copy constructor? Justify
In both case it will call copy constructors
 
Is this answer useful? Yes | No
July 27, 2009 20:08:18   #4  
lddung19720603 Member Since: July 2009   Contribution: 1    

RE: While copying the objects if we say X a=b, X a(b) What will it chttp://www.geekinterview.com/vblogin.php?qid=53729&redirect=/question_details/53729/reply&tredirect=http://www.geekinterview.com/question_details/53729all, assignment operator or copy con
In both cases the copy constructor will be called.
If we have a b alone then the assignment operator will be called.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape