Why is the copy constructor of the string used?
<p>I have the following code:</p>

<pre><code>class TestClass
{
public:
  TestClass(){};

  std::string GetTestString()
  {
    return (mTestString);
  }
  void SetTestString(const std::string&amp; rTestString)
  {
    mTestString = rTestString;
  }

private:
  std::string mTestString;
};

TestClass* pGlobalVar;

void SomeFunction(TestClass MyClass)
{
  pGlobalVar-&gt;SetTestString("cba");
  std::cout &lt;&lt; "Changed string:  " &lt;&lt; pGlobalVar-&gt;GetTestString() &lt;&lt; std::endl;
  std::cout &lt;&lt; "Copied string:   " &lt;&lt; MyClass.GetTestString() &lt;&lt; std::endl;

}

int main()
{
  pGlobalVar = new TestClass();
  pGlobalVar-&gt;SetTestString("abc");
  std::cout &lt;&lt; "Original string: " &lt;&lt; pGlobalVar-&gt;GetTestString() &lt;&lt; std::endl;
  SomeFunction(*pGlobalVar);
  delete (pGlobalVar);
}
</code></pre>

<p>This outputs the following:</p>

<pre>Original string: abc
Changed string:  cba
Copied string:   abc
</pre>

<p>As I did not define a copy constructor for my class, I would expect that a flat copy would be made, including the pointer in the <code>std::string</code>. Apparently though the <code>std::string</code> copy constructor is used, since a change to the original string did not change the copy. </p>

<p>Can anyone explain to me why it did not make flat copy?</p>

<p>I'm using Linux with GCC 4.4.6.</p>
