title: C++ Can you cast an object using a reference
body: <p>I have a quick question which is probably a bit silly, but hopefully someone might be able to clear things up for me.</p>
  
  <p>Basically, I have a class 'Test' that has a parameter object stored of type 'A', where there are several other classes like 'B', 'C', 'D' which all extend 'A'.</p>
  
  <p>I am wondering if I can use this parameter (ObjectType) to create a new object of that type, i.e. use it when casting.</p>
  
  <p>I can do the following to create a new object of the parent type </p>
  
  <p><code>AA* NewObject = SpawnObject&lt;AA&gt;();</code></p>
  
  <p>But I am wondering if I can do anything that allows me to use the parameter I have stored, i.e. if the parameter is an object type 'B' it would know to do:</p>
  
  <p><code>AA* NewObject = SpawnObject&lt;AB&gt;();</code></p>
  
  <p>i.e. essentially I am asking if there is any way to achieve the following, where ObjectType is the stored parameter</p>
  
  <p><code>AA* NewObject = SpawnObject&lt;ObjectType&gt;();</code></p>
  
  <p>Any help would be greatly appreciated.</p>