title: Swig typecast to derived class?
body: <p>I notice that Swig provides a whole host of functions to allow for typecasting objects to their parent classes. However, in C++ one can produce a function like the following:</p>
  
  <pre><code>A * getAnObject()
  {
    if(someBoolean)
      return (A *) new B;
    else
      return (A *) new C;
  }
  </code></pre>
  
  <p>Where "A" is the parent of classes "B" and "C". One can then typecast the pointer returned into being a "B" type or "C" type at one's convenience like:</p>
  
  <pre><code>B * some_var = (B *) getAnObject();
  </code></pre>
  
  <p>Is there some way I can typecast an object I've received from a generic-pointer-producing function at run-time in the scripting language using the wrappers? (In my case, Lua?) I have a function that could produce one of about a hundred possible classes, and I'd like to avoid writing an enormous switch structure that I'd have to maintain in C++. At the point where I receive the generic pointer, I also have a string representation of the data type I'd like to cast it to.</p>
  
  <p>Any thoughts? Thanks!</p>
  
  <p>-- EDIT --</p>
  
  <p>I notice that SWIG offers to generate copy constructors for all of my classes. If I had it generate those, could I do something like the following?: </p>
  
  <pre><code>var = myModule.getAnObject(); -- Function that returns an object type-cast down to a pointer of the parent class, as in the function getAnObject() above.
  var = myModule.ClassThatExtendsBaseClass(var); -- A copy constructor that SWIG theoretically creates for me
  </code></pre>
  
  <p>and have var then be an instance of the inheriting class that <em>knows</em> it's an instance of the inheriting class?</p>