title: c++ pointer arguments: does it make sense to create a copy of the value pointed too, then store the copy as pointer again?
body: <p>I'm migrating a project to c++ because I hit a performance ceiling while developing it in c#. It's my first time using c++, however, and I'm finding myself doing something a lot that doesn't seem quite right...</p>
  
  <p>Consider the following abstracted example:</p>
  
  <pre><code>class ClassC
  {
      ClassC::ClassC(int option)
      {
          //do something
      }
  }
  
  class ClassB
  {
      ClassC* objC
  
      ClassB::ClassB(ClassC* objC)
      {
          this-&gt;objC = new ClassC(*objC);
      }
  }
  
  class ClassA
  {
      void functionA(void)
      {
          ClassB objB (&amp;ClassC(2));
      }
  }
  </code></pre>
  
  <p>ClassA has a function which creates a ClassB. ClassB's constructor accepts a ClassC, objC. objC is <em>passed</em> by reference because ClassC is not a primitive type, but its <em>stored</em> by reference because ClassC has no default constructor. However, because objC is created in static memory and will be destructed when functionA completes, ClassB needs to copy to the value pointed by objC into dynamic memory, then store a pointer to that copy.</p>
  
  <p>This seems very round-a-bout to me, and makes me feel like im approaching something incorrectly. Is this a standard thing to do in c++?</p>
  
  <p><strong>EDIT: Everyone seems to be saying that the line <code>ClassB objB (&amp;ClassC(2));</code> is incorrect because the value of the ClassC object will be lost before ClassB can make a copy of it. But I have compiled my example and this is not the case. Here is revised, working code:</strong></p>
  
  <pre><code>class ClassC
  {
      int option;
  
  public:
      ClassC::ClassC(int option)
      {
          this-&gt;option = option;
      }
  
      int ClassC::getOption(void)
      {
          return option;
      }
  };
  
  class ClassB
  {
      ClassC* objC;
  
  public:
      ClassB::ClassB(ClassC* objC)
      {
          this-&gt;objC = new ClassC(*objC);
      }
  
      int ClassB::getOption(void)
      {
          return objC-&gt;getOption();
      }
  };
  
  class ClassA
  {
  public:
      static ClassB functionA(void)
      {
          return ClassB (&amp;ClassC(2));
      }
  };
  
  int main(void)
  {
      ClassB objB = ClassA::functionA();
  
      int test = objB.getOption(); //test = 2, therefore objC was copied successfully.
  
      return 0;
  }
  </code></pre>