The program below compiles and runs as it should. However the destructor is being called twice, and I can't figure out why? Can you take a look and help me?
The output is:
"Your profile is created!
Arya Stark
Customer Number: 7 - Cost of the fare:180.00 - Customer Points: 4
Good Effort
Name: Arya Stark- Fee: 12- Rating: 0.10
Good Effort"
The uber_call function takes its arguments by value (which is the default in C++ for all types).
This means that the function will receive a copy of the UberDriver object that you pass to it.
If you want to avoid creating a copy you need to change function to take the argument by reference, like so:
void uber_call(const UberDriver& driver, double km) {
const isn't strictly necessary but it's usually a good idea when the function is not supposed to modify the object.
On line 63 the parameter driver is a copy of the provided object, hence it is constructed and at the end of the function destructed. If you don't want this copy you may pass a const reference:
void uber_call(const UberDriver &driver, double km) {
The first one is the destructor of 'driver' on line 82. This UberDriver was created on line 63 with copy construction due to the function call on line 89.
The second one is the destructor of 'driver' on line 91. This UberDriver was created on line 85.
You do pass argument to function by value. That is a copy operation. (Could be a move in current C++11, but there would still be distinct objects.)