Given the next code:
#include <iostream>
#include <vector>
#include <cstring>
class Person
{
public:
Person(const char *i_Name = "Unknown", int i_Age = 0)
: m_Name(copyFromTo(i_Name, m_Name)), m_Age(i_Age)
{
std::cout << "creating obj " << this << ".\n";
}
Person(const Person &other)
{
std::cout << "Copying from " << &other << " to " << this << ".\n";
this->m_Name = copyFromTo(other.m_Name, this->m_Name);
this->setAge(other.m_Age);
}
~Person()
{
std::cout << "destroying obj " << this << ".\n";
if (m_Name)
{
delete[] m_Name;
}
}
void printDetails() { std::cout << "Hello, my name is " << m_Name << " and I am " << m_Age << " years old.\n"; }
void setAge(int i_Age) { m_Age = i_Age; }
int getAge() const { return m_Age; }
private:
char *copyFromTo(const char *i_Source, char *i_Destination)
{
int sourceSize = strlen(i_Source);
i_Destination = new char[sourceSize + 1];
memcpy(i_Destination, i_Source, sourceSize);
i_Destination[sourceSize] = '\0';
return i_Destination;
}
char *m_Name;
int m_Age;
};
Person birthday(Person person)
{
std::cout << "person's address inside the function " << &person << ".\n";
person.setAge(person.getAge() + 1);
return person;
}
int main()
{
Person person = {"New Person", 24};
birthday(person);
std::cout << "END OF PROGRAM.\n";
}
And the next output:
Output:
creating obj 0x61fea8.
Copying from 0x61fea8 to 0x61feb8.
person's address inside the function 0x61feb8.
Copying from 0x61feb8 to 0x61feb0. // [temporary object creation]
destroying obj 0x61feb0. // [temporary object destruction]
destroying obj 0x61feb8.
END OF PROGRAM.
destroying obj 0x61fea8.
Notice that the program creates an object as soon as we enter the function 'birthday' because the argument 'person' was passed by value. However, at the end of the function, there is another call to the copy constructor, and we are creating an additional temporary object that we destroy immediately afterward. Why do we need this temporary object in addition to the local 'person' object inside the 'birthday' function if we destroy it before we destroy the local 'person' object.
Thanks.
I tried google and saw that before the function ends there is creation of a temporary object but I did not understand why do I need it if I destroy it even before I destroy the first local object of the function. What was it used for actually?
If you modify your main function to:
You will see that the address of your "second temporary" will be the same as the local variable
anotherPerson.What happens: If a function must return a value, it must be valid after the function has left its scope. Current C++ guarantees that these copy will be elided and the "new" place for your local variable ( here in the scope of
main) will be used for this "copy" which is in fact no copy anymore. This kind of optimization is called copy elisionIt is quite interesting that an unused return value produces also a unused temporary. But that is not anything which the C++ standard tells us. This is only an observation of the compiler you are using.
BTW: Your init and copy of your string is fully broken and the variable is used uninitialized! Simply enable all warnings and don't try to be better as the standard behavior of
std::stringis already!