I am wondering why in the following C++ code the copy constructor is called 25 times for 10 iterations?
If it was 10 then OK 10/10 = 1
, or 20/10 = 2
, or 30/10 = 3
, but 25/10 = 2.5
? What does the .5
here mean?
Header:
class Person
{
public:
Person(std::string name, int age);
Person(const Person &person);
const std::string &getName() const;
int getAge() const;
private:
std::string name;
int age;
};
Source:
Person::Person(string name, int age) : name(std::move(name)), age(age)
{}
Person::Person(const Person &person)
{
this->name = person.name;
this->age = person.age;
static int count = 0;
count++;
cout << ">>Copy-Person::Person(Person &person) " << count << endl;
}
const string &Person::getName() const
{
return name;
}
int Person::getAge() const
{
return age;
}
Usage:
int main()
{
vector<Person> persons;
for (int i = 0; i < 10; ++i)
{
Person person(to_string(i + 1), i);
persons.push_back(person);
}
cout << "-----------------------------------------------" << endl;
for (Person &person : persons)
{
cout << "name = " << person.getName() << " age = " << person.getAge() << endl;
}
return 0;
}
Output:
>>Copy-Person::Person(Person &person) 1
>>Copy-Person::Person(Person &person) 2
>>Copy-Person::Person(Person &person) 3
>>Copy-Person::Person(Person &person) 4
>>Copy-Person::Person(Person &person) 5
>>Copy-Person::Person(Person &person) 6
>>Copy-Person::Person(Person &person) 7
>>Copy-Person::Person(Person &person) 8
>>Copy-Person::Person(Person &person) 9
>>Copy-Person::Person(Person &person) 10
>>Copy-Person::Person(Person &person) 11
>>Copy-Person::Person(Person &person) 12
>>Copy-Person::Person(Person &person) 13
>>Copy-Person::Person(Person &person) 14
>>Copy-Person::Person(Person &person) 15
>>Copy-Person::Person(Person &person) 16
>>Copy-Person::Person(Person &person) 17
>>Copy-Person::Person(Person &person) 18
>>Copy-Person::Person(Person &person) 19
>>Copy-Person::Person(Person &person) 20
>>Copy-Person::Person(Person &person) 21
>>Copy-Person::Person(Person &person) 22
>>Copy-Person::Person(Person &person) 23
>>Copy-Person::Person(Person &person) 24
>>Copy-Person::Person(Person &person) 25
-----------------------------------------------
name = 1 age = 0
name = 2 age = 1
name = 3 age = 2
name = 4 age = 3
name = 5 age = 4
name = 6 age = 5
name = 7 age = 6
name = 8 age = 7
name = 9 age = 8
name = 10 age = 9