0
class person { std::string name; int age; public: person(const std::string& name, int age) : name(name), age(age) { } }; int main() { person a("Bjarne Stroustrup", 60); person b(a); // What happens here? b = a; // And here? } 

Why constructor with 2 argument parameter accepts copy object as parameter. We calling constructor with 1 argument person b(a) with different type and it works?

How ?

1

4 Answers 4

3

It doesn't. This line of code:

person b(a); 

invokes implicitly defined person's copy constructor. The one generated by a compiler. It would also invoke a copy constructor if you had:

person b = a; 

That constructor accepts one parameter of type person&‍, const person&‍, volatile person&‍ or const volatile person&. In your case that would be the object a of type person. It does not call the following constructor:

person(const std::string& name, int age) 
Sign up to request clarification or add additional context in comments.

2 Comments

is this some kind of implicit function overloading?
No, this is overloading when one of the candidates are implicitly generated.
2
person b(a); 

This calls the copy constructor of person that's generated by the compiler for you. The compiler generated version looks somewhat like this (naive version) :

person(const person& other) { name = other.name; age = other.age; } 

b = a; // And here? 

This calls the copy assignment operator which is also generated by the compiler for you which does more or less the same as the copy constructor in this case.

1 Comment

@Kad Yeah, the compiler picks the compiler-generated copy-constructor.
1

The compiler usually generates some constructors for you, including the copy constructor person::person(const person &) used by person b(a); and assignment operators, including the copy assignment operator person & person::operator=(const person &) used by b = a;.

Comments

0

A good learning exercise is that if you don't know what your program automatically do, implement it yourself to understand how things work. Here is a good website that explains the issue you have. It's about how the constructors works in C++

class person { std::string name; int age; public: person(const std::string& name, int age) : name(name), age(age) { } /** * If you do nothing this is what is automaticaly impremented */ personne(const person p) { this.name = p.name; // or p.getName() this.age = p.age; // or p.getAge() } person person::operator=(const person & p) { if(this == &p) return *this; this.name = p.name; // or p.getName() this.age = p.age; // or p.getAge() return *this; } }; int main() { person a("Bjarne Stroustrup", 60); person b(a); // copy constructor b = a; // assignment operator overloading } 

9 Comments

"if you don't know what your program automaticaly do, implement it yourself" - Not a good practice. You're much more likely to make mistakes than your compiler when implementing these. A good practice would be to explicitly default or delete these using = default or = delete.
It's a good way to practice, and build understanding. It's not a good practice. :-)
"if you don't know what your program automaticaly do, implement it yourself" The asker did not know about the existence of automatically generated constructors etc. - how are they supposed to reimplement them when they are unaware of them?
@Holt I did not say to do it everytime but Kad who asked the question doesn't look like someone that understand what happend while an object is instancied. I just said that as a way of practice
@MaxLanghof that's why I implemented it in the example
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.