title: Passing derived class to a function of base class argument
body: <p>I have this code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Base {
    public:
    virtual void sayHello() {
      std::cout &lt;&lt; "Hello world, I am Base" &lt;&lt; std::endl;
    }
  };
  
  class Derived: public Base {
    public:
    void sayHello() {
      std::cout &lt;&lt; "Hello world, I am Derived" &lt;&lt; std::endl;
    }
  };
  
  void testPointer(Base *obj) {
    obj-&gt;sayHello();
  }
  
  void testReference(Base &amp;obj) {
    obj.sayHello();
  }
  
  void testObject(Base obj) {
    obj.sayHello();
  }
  
  int main() {
    {
      std::cout &lt;&lt; "Testing with pointer argument: ";
      Derived *derived = new Derived;
      testPointer(derived);
    }
    {
      std::cout &lt;&lt; "Testing with reference argument: ";
      Derived derived;
      testReference(derived);
    }
    {
      std::cout &lt;&lt; "Testing with object argument: ";
      Derived derived;
      testObject(derived);
    }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Testing with pointer argument: Hello world, I am Derived
  Testing with reference argument: Hello world, I am Derived
  Testing with object argument: Hello world, I am Base
  </code></pre>
  
  <p>My question is why both the pointer case <code>void testPointer(Base *obj)</code> and the reference case <code>void testReference(Base &amp;obj)</code> return the result of derived instance of <code>void sayHello()</code> but not and the pass by copy case? What should I do to make the copy case to return the result of the derived class function <code>void sayHello()</code>?</p>