title: A way how to overcome object slicing
body: <p>Is there a way how to step away , overcome object slicing without using new keyword as paramater to function? I have base object</p>
  
  <pre><code>class Person{
      public:
          Person(string name , int age ){
              this -&gt; name = name;
              this -&gt; age  = age;
          }
          virtual void getInfo(){
              cout &lt;&lt; "Person " &lt;&lt; name &lt;&lt; " " &lt;&lt; age;
          }
          void add( string name , Person a){
              Person *one = new Person(a);
              m[ name ] = one;
          }
          void print(){
              for( auto &amp;x: m )
                   x.second -&gt; getInfo()
          }
      protected:
          string name;
          int age;
         map&lt; string , Person*&gt; m;
      };
  </code></pre>
  
  <p>Derived objects</p>
  
  <pre><code>class Kid : public Person{
  public:
      Kid(string name, int age):Person(name,age){};
      virtual void getInfo( ){
          cout &lt;&lt; "Kid " &lt;&lt; name &lt;&lt; " " &lt;&lt; age;
      }
  };
   class Adult : public Person{
      public:
          Adult(string name, int age):Person(name,age){};
          virtual void getInfo( ){
              cout &lt;&lt; "Adult " &lt;&lt; name &lt;&lt; " " &lt;&lt; age;
          }
      };
  </code></pre>
  
  <p>And trying to invoke it such as</p>
  
  <pre><code>   Person one("John", 25);
     one.add("first",Kid("Mathew",15));
     one.add("second",Adult("Leo",55));
     one.print();
  </code></pre>
  
  <p>Due to object slicing it prints </p>
  
  <blockquote>
    <p>"Person..."</p>
  </blockquote>
  
  <p>I have tried using <code>unique_ptr</code> and ended up with same result.
  I tried declaring <code>copy function</code> such as</p>
  
  <pre><code>vitual clone() const{ return new Person(*this);}
  </code></pre>
  
  <p>in base clase</p>
  
  <p>and in derived classes i declared</p>
  
  <pre><code>Kid* clone() const{ return new Kid(*this);}
  Adult* clone() const{ return new Adult(*this);}
  </code></pre>
  
  <p>and edited add function.</p>
  
  <pre><code>    void add( string name , Person a){
          Person *one = a.clone();
          m[ name ] = one;
      }
  </code></pre>
  
  <p>And i was still strucked down by a object slicind. I have tried various things/approaches/methods and all led to object slicing.</p>
  
  <p>Is there a way how to create such thing ( map that holds derivec classes of base class ) without using <code>new</code> keyword in the argument of function e.g</p>
  
  <pre><code>Person one("bla",5);
  one.add("asd", new Kid("one",15))
  </code></pre>
  
  <p>Thanks for help.</p>
  
  <p>//
  Current state of the code</p>
  
  <pre><code>class Person{
      public:
          Person(string name , int age ){
              this -&gt; name = name;
              this -&gt; age  = age;
          }
          virtual void getInfo(){
              cout &lt;&lt; "Person " &lt;&lt; name &lt;&lt; " " &lt;&lt; age;
          }
          void add( string name , const Person &amp;a){
              Person *one = a.clone();
              m[ name ] = one;
          }
          void print(){
              for( auto &amp;x: m ){
                   x.second -&gt; getInfo();
                   cout &lt;&lt; endl;
              }
          }
           virtual Person* clone() const{ return new Person(*this);}
  
      protected:
          string name;
          int age;
          map&lt; string , Person*&gt; m;
      };
  
  class Kid : public Person{
      public:
          Kid(string name, int age):Person(name,age){};
          virtual void getInfo( ){
              cout &lt;&lt; "Kid " &lt;&lt; name &lt;&lt; " " &lt;&lt; age;
          }
          Kid* clone() const{ return new Kid(*this);}
      };
   class Adult : public Person{
      public:
          Adult(string name, int age):Person(name,age){};
          virtual void getInfo( ){
              cout &lt;&lt; "Adult " &lt;&lt; name &lt;&lt; " " &lt;&lt; age;
          }
          Adult* clone() const{ return new Adult(*this);}
      };
  
  int main(){
       Person one("John", 25);
     one.add("first",Kid("Mathew",15));
     one.add("second",Adult("Leo",55));
     one.print();
  }
  </code></pre>