If I have two classes A and B and I do A=B which assignment constructor is called? The one from class A or the one from class B?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s copy constructor and there’s assignment operator. Since
A != B, the copy assignment operator will be called.Short answer:
operator =from class A, since you’re assigning to class A.Long answer:
A=Bwill not work, sinceAandBare class types.You probably mean:
In which case,
operator =forclass Awill be called.The conversion constructor will be called for the following case:
where A is defined as:
Note that this introduces implicit casting between B and A. If you don’t want that, you can declare the conversion constructor as
explicit.