I have two copy constructors
Foo(Foo &obj){
}
Foo(Foo *obj){
}
When will the second copy constructor will get called?
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.
Leaving aside that the second constructor isn’t a copy constructor – you actually wanted to know when the second constructor will be called.
The
Foo(Foo* obj);constructor is a single parameter constructor – because it hasn’t been marked with theexplicitkeyword, it provides an implicit conversion fromFoo*toFoo. It may be called at any time where aFoo*is used in the place of aFooorconst Foo&– if it’s being called unexpectedly, that’s almost certainly what’s happening.In general, single parameter constructors should either be copy constructors (which other answers have explained) or should be marked
explicit. Constructors which provide implicit conversions should be used sparingly.