title: How to cast pointer to base type conditionally given a pointer to derived type
body: <p>I have two derived classes, <code>Triangle</code> and <code>Sphere</code>, from base class <code>Shape</code>.</p>
  
  <p>I have a situation where I stored a base class pointer, <code>Shape*</code> in class <code>A</code>, and have two overloaded member functions of class <code>A</code> for each derived type, <code>doSomething(Sphere* sp)</code> and <code>doSomething(Triangle* tr)</code>.</p>
  
  <pre><code>class Shape
  {
  public:
      Shape() {};
      virtual ~Shape() = 0;
  };
  
  class Triangle : public Shape
  {
  public:
      Triangle();
  };
  
  class Sphere : public Shape
  {
  public:
      Sphere();
  };
  
  class A
  {
  private:
      Shape* shape;
      void doSomething(Triangle* tr);
      void doSomething(Sphere* sp);
  public:
      A();
      ~A();
  };
  </code></pre>
  
  <p>However, when I try to run my code by passing pointer of type <code>Shape*</code> into <code>doSomething</code> function, I get following error:</p>
  
  <pre><code>candidate function not viable: cannot convert from base class pointer 'value_type'
        (aka 'Shape *') to derived class pointer 'const Triangle *' for 1st argument
  void A::doSomething(Triangle* tr) {
  </code></pre>
  
  <p>And same message for <code>Sphere</code>. I'm also sure that although the pointer of type <code>Shape *</code>, that it actually points to one of the derived objects. </p>
  
  <p><strong>So, how could I resolve this issue preferably without modifying <code>Triangle</code> and <code>Sphere</code> classes and only modifying <code>A</code>?</strong></p>