title: Best way for creating a generic object in C++?
body: <p>Having programmed mostly in C# I find myself in a loss when it comes to C++.  I however need to create a C++ app, as it is merely a component in a bigger C++ solution.</p>
  
  <p><strong>Situation</strong></p>
  
  <ul>
  <li>I have a struct (Parent) that include a data component (object).  This could be a data component of any type - i.e. custom struct too.</li>
  <li>Only the compiler of the Parent (who created the Parent incl object), and the end receiver needs to know the type of the data component inside the object, as the data is only relevant to them.</li>
  <li>The Parent struct however might pass through several methods, objects and even to other processes.</li>
  <li>The data options for the object are limited, and both compiler and decompiler knows the different options of data types.. hence they are able to decompile the object in original form</li>
  <li>The types however could be extended (i.e. though limited it is not necessarily fixed) and the decompiler and compilers updated in future</li>
  </ul>
  
  <p><strong>Problem</strong>
  -  I do not want to recreate each method and object that might encounter this data component with a "template"... also seeing that the data type could in future be altered suggested that templates for each process that encounter the object in the long run is not ideal to start with.</p>
  
  <p>I was looking for something similar to C# Object - and have written the following components</p>
  
  <p><strong>Question</strong></p>
  
  <ul>
  <li>Is this a good solution or may I encounter problems in the future</li>
  <li>Is there an improvement that could be made on this? That I have not thought about (especially for not including the Object\impl.h?</li>
  <li>Is there a totally different / better solution?</li>
  </ul>
  
  <p><strong>The Code</strong></p>
  
  <ul>
  <li><p>I have the following in my Header file</p>
  
  <pre><code>struct Object
  {
      Object();
      // Return true if value has succesfully been set
      // Return false if there is no compatibility between Value and result.
      template &lt;typename T&gt;
      bool GetValue(T &amp;result);
      template&lt;typename T&gt;
      bool SetValue(T value);
      virtual LPVOID GetObjectAddress() = 0;
      virtual const char* GetType() = 0;
  };
  
  template&lt;typename T&gt;
  struct ObjectType:public Object
  {
          ObjectType(T value);
          T Value;
          LPVOID GetObjectAddress();
          const char* GetType();
  };
  </code></pre></li>
  </ul>
  
  <p>I also have have a CreateType function for creating several ready made objects at start and for example purposes is called as follows at end of .h file</p>
  
  <pre><code>    template class CreateType&lt;int&gt;;
  </code></pre>
  
  <p>Bear in mind the int is just for example... it is actually different structs.</p>
  
  <ul>
  <li><p>I also have another header file which is included at the bottom of this header file as follows:</p>
  
  <pre><code>#include "Implementation\Object_impl.h"
  -&gt; looks like this
  
  template&lt;typename T&gt;
  ObjectType&lt;T&gt;::ObjectType(T value)
  {
      Value = value;
  };
  
  template &lt;typename T&gt;
  // Return true if value has succesfully been set
  // Return false if there is no compatibility between Value and result.
  bool Object::GetValue(T &amp;result)
  {
      if (typeid(result).name() == GetType())
      {
          result = *(T *)GetObjectAddress();
          return true;
      }
      return false;
  };
  
  template&lt;typename T&gt;
  bool Object::SetValue(T value)
  {
      if (typeid(T).name() == GetType())
      {
          *(T*)GetObjectAddress() = value;
          return true;
      }
      return false;
  };
  
  template&lt;typename T&gt;
  const char* ObjectType&lt;T&gt;::GetType()
  {
      return typeid(Value).name();
  }
  template&lt;typename T&gt;
  EXPOBJ LPVOID ObjectType&lt;T&gt;::GetObjectAddress(){
      return (LPVOID)&amp;Value;
  }
  </code></pre></li>
  </ul>
  
  <p>I wish I could include most of this in a cpp file, but then I would not be able to create different object on demand... I am not yet sure what the implications would be... also to extend the types one only need to include the normal object header file.</p>
  
  <p>I know inline could be an option, but also think it is not ideal?</p>
  
  <p>At the moment however this compiles and works perfectly as a "generic" option.  One could also extend by inheriting from inheriting from "Object"?</p>
  
  <p>...oh and to use I just do this - which seemed to work:</p>
  
  <pre><code>    Object * a;
      a = new ObjectType&lt;testing&gt;(testing());
      testing x = testing();
      x.i = 50;
      a-&gt;SetValue(x);
      testing y = testing();
      testing &amp;z = y;
      a-&gt;GetValue(z);
  
      cout &lt;&lt; z.i &lt;&lt; " for z and y = " &lt;&lt; y.i &lt;&lt; endl;
  
      Result -&gt; 50 for z and y = 50
  </code></pre>