I want to be sure I understand correctly. In the c++ functions below three instances of
std::string will be created:
- one for s1 in caller() via what I believe is called the assignment constructor
- one for the s2 parameter of the callee() function via it’s copy constructor
- one for s3 via its copy constructor
Am I correct? And if so will all three instances be cleaned up as they go out of scope? I’m not really asking if this is good code or not, just if my understanding is correct.
void caller(void) {
std::string s1 = "hi";
callee(s1);
}
void callee(std::string s2) {
std::string s3 = s2;
}
You are almost correct.
Either three or four strings may be created (depending on whether the construction of
s1is elided), and in each case a constructor is called to construct them. Despite appearances, there are no calls to any assignment operators.