Parameter Passing¶
Given the following (partial) class definitions:
class Person
{
public:
Person(std::string firstname,
std::string lastname);
};
class Person
{
public:
Person(const std::string& firstname,
const std::string& lastname);
};
Question 1: Which One Is Better?¶
Which of these class definitions would lead to better performance in object initialization?
Variant A |
Variant B |
---|---|
Question2: Why?¶
Why does the chosen variant perform better? (Write an X
in the
“True” or “False” columns)
True |
False |
Statement |
---|---|---|
Variant A has a shorter calling sequence |
||
Variant A is better at moving the parameters into the object |
||
Variant B does not create temporary copies |