There are no issues with your code. When you call
dislplaymessage()
from within
showmessage()
, a copy of the object
referenced by shomessage() argument
str
is created,
nota copy of the reference. This can be demonstrated simply:
#include <iostream>
class C
{
public:
C(){
std::cout << "constructor\n";
}
C(const C &other) {
std::cout << "copy constructor\n";
}
};
void foo(C c)
{
std::cout << "foo\n";
}
void bar(C& c)
{
std::cout << "bar\n";
foo(c);
}
int main()
{
C c;
bar(c);
}
produces the output
constructor
bar
copy constructor
foo