title: How to create/destroy objects in "modern" C++?
body: <p>I am porting C# app into C++ linux app. I am confused with construction and destruction in &quot;modern&quot; (C++11 ?). I always thought you have to use <code>new/delete</code> but now it appears C++ recommendation is not to do so (is this correct or I read a wrong blog?).</p>
  <p>What I have roughly (there are 6 subclasses of <code>B</code> and <code>BRec</code> atm):</p>
  <pre><code>class ARec : public BRec
  class A : public B
  . . .
  class Factory
    BRec GetRecord(WrapperStruct s)
    {
      if(s.Type == 'A')
      {
        A* a = (A*)s.Data;
        auto rec = ARec((ushort)a-&gt;Num);
        rec.OtherField = a.OtherField;
        return rec;
      }
  . . .
  main
  
  // separate pthread
  void* Logger(void* arg) {
  
      int* thread_state = (int*)arg;
  
      auto f = Factory();
  
      WrapperStruct item;
  
      while (true)
      {
        q-&gt;try_dequeue(item);
  
        auto rec = f.GetRecord(item);
        auto bytes = f.GetBytes(rec);
  
        // write bytes to file ...
  
        delete bytes;
  
        if(*thread_state == -1)
          pthread_exit(0);
      }
  </code></pre>
  <p>Question - how does compiler would know when to delete <code>s.Data</code> in factory method? Same - <code>rec</code> if it was created in a class method and then crossed to other method (<code>while</code> in Logger)? Is there a simple guide how to manage variables' memory in C++11 ?</p>
  <p><strong>EDIT:</strong> <em><code>s.Data</code> contains ref to an object created in a 3rd party dll and can be of different types hence the <code>s.Type</code> field</em></p>