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 ?

Kad
  • 542
  • 1
  • 5
  • 18
  • 4
    I suggest you get [a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) as they will tell you what's going on. – Some programmer dude Jun 19 '18 at 13:12

4 Answers4

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)
Ron
  • 14,674
  • 4
  • 34
  • 47
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.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
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;.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
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 
}
Hearner
  • 2,711
  • 3
  • 17
  • 34
  • 1
    *"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`. – Holt Jun 19 '18 at 13:24
  • 1
    It's a good way to practice, and build understanding. It's not a good practice. :-) – super Jun 19 '18 at 13:25
  • 1
    *"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? – Max Langhof Jun 19 '18 at 13:26
  • @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 – Hearner Jun 19 '18 at 13:27
  • @MaxLanghof that's why I implemented it in the example – Hearner Jun 19 '18 at 13:27
  • @Hearner "A good practice" in English usually means something like "A good general rule to follow". If interpreted that way it's not very good advice. Changing the wording might clarify what you mean. – super Jun 19 '18 at 13:34
  • As a non english speaker i apology – Hearner Jun 19 '18 at 13:35
  • @Hearner I wasnt know that compiler generates an calls copy constructor itself – Kad Jun 19 '18 at 13:40
  • @Kad no problem at all, you should look that the website I linked you. Many things are automatics and you don't even know it. The problem is that for some rare cases it may cause errors. Create your own constructor anyway and you'll avoir every issues – Hearner Jun 19 '18 at 13:42