Link to home
Start Free TrialLog in
Avatar of tommym121
tommym121Flag for Canada

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.


}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tommym121

ASKER

Thanks.