Rvalues and constructor calls

Pages: 12
andywestken wrote:
Constructor called [0, init, -1, mottoA]
length #2 = 32
Constructor called [1, init, -1, mottoB]
length #2 = 40
Constructor called [2, init, -1, mottoC]
length #2 = 17

Executing: mottoC = mottoA + mottoB.
Add operator overload called [0, init, -1, mottoA] add [1, init, -1, mottoB]
length #1 = 73
Constructor called [3, init, -1, mottoTemp] <------ c'tor mottoTemp
length #2 = 72
Move copy constructor called [4, move, 3, anon]
from [3, init, -1, mottoTemp] <----- move c'tor of anon temp (moves data from mottoTemp to anon)
Destructor called [3, init, -1, mottoTemp] <----- destroy mottoTemp (the data is not destroyed, as it's been moved elsewhere)
Move assignment operator overload called [2, init, -1, mottoC]
from [4, move, 3, anon] <----- assigns from anon temp, not mottoTemp, so data not gone
Destructor called [4, move, 3, anon] <----- destroy anon temp (data already gone...)
Done!

I am Ozymandias, king of kings. Look on my works ye mighty and despair.

Destructor called [2, init, -1, mottoC]
Destructor called [1, init, -1, mottoB]
Destructor called [0, init, -1, mottoA]
R10111001 wrote:
Constructor called.
Constructor called.
Constructor called.

Executing: motto3 = motto1 + motto2.
Add operator overload called.
Constructor called. <----- c'tor for local object Message
Move copy constructor called. <----- move c'tor of a temporary object. moves the data from local object Message to another temp object (rvalue)
Destructor called. <----- Local object Message is first set to nullptr and then destroyed
Move assignment operator overload called. <----- Moves the data from the temp object (rvalue) to Motto3.pmessage
Destructor called.
Done!

I am Ozymandias, king of kings. Look on my works ye mighty and despair.

Destructor called.
Destructor called.
Destructor called.


Yes and what I think what the reason for this temporary object is that the assigment is considered to be outside the function hence all local variables are destroyed and a temporary object is needed to transport the value.
Topic archived. No new replies allowed.
Pages: 12