On Sunday, 20 November 2016 18:18:35 UTC+2, Christian Steins wrote:
> Hi,
> following program gives this output:
>
> name = c1 x = 1 y = 2
> name = c2 x = 3 y = 4
> destructor c1
> destructor c2
> destructor c1
> destructor c1
> destructor c2
>
> I don't understand why the destructor is called 5 times and the sequence.
Why don't you explain what you did expect and in what order and why?
Then we can see where your logic fails. For me 5 destructor calls
seems like expected and order seems OK.
After 'myvec.push_back(c1);' the vector has one element and capacity for
one element so 'myvec.push_back(c2);' causes it to allocate more storage,
copy 'c1' there and to destroy the old storage and so call destructor for
that 'c1' in it.
After that the 'main' ends that causes destruction of original 'c2'
original 'c1' and then 'myvec' and so copies of 'c1' and 'c2' in that
'myvec'. So 5 calls.