tommym121
asked on
Returning an object from a function
I am a bit confuse with object return in general. Am I right the copy constructor being called for the object when a function return the object.
For example,
std::string ReturnStringFunction (std::string str)
{
std::string s = str;
return s;
}
Now in the main program
main()
{
std::string s1;
std::string s2;
s1 = ReturnStringFunction ("DummyString1");
s1 = ReturnStringFunction ("Dummy2");
// ??? am I protentially introduce a memory leak here?
s2 = ReturnStringFunction ("Dummy2");
// ??? am I introduce a different copy of "Dummy2", 2 different memory location.
}
For example,
std::string ReturnStringFunction (std::string str)
{
std::string s = str;
return s;
}
Now in the main program
main()
{
std::string s1;
std::string s2;
s1 = ReturnStringFunction ("DummyString1");
s1 = ReturnStringFunction ("Dummy2");
// ??? am I protentially introduce a memory leak here?
s2 = ReturnStringFunction ("Dummy2");
// ??? am I introduce a different copy of "Dummy2", 2 different memory location.
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER