I am tring to understand understand what the following program does.
I am assuming the following:
1.From line 26 "John Done" argument is passed to the accountName in the line 8.
2.accountName initializes the name data member in line 9.
3. The next step is to create the setName member function but I am not sure how the accountN in line 13 gets the argument. I know the setName is a member function that sets the account name in the object.
Could someone explaine how the argument "John Doe" passed to the setName function?
3. The next step is to create the setName member function
No. In this example, setName() is not used.
Account account1{"John Doe"};
calls the constructor as per steps 1 & 2.
cout << account1.getName();
calls .getName() member function.
setName() although defined in the class is not used.
Also note that when passing containers (such as string) as a function argument, these are usually passed by ref and not by value as here. Passing by value causes a copy to be undertaken which is not neccessary here.
Passing by value causes a copy to be undertaken which is not neccessary here.
As long as you move from the parameter, passing by-value is actually not horrible since the data must be copied once anyway. The difference is merely one additional move.
This is suboptimal, but to correct it you'll need to throw away that overrated DRY principle and essentially write the same code twice - by adding a redundant move constructor.
Get optimal behavior by passing string_view instead.