title: Is it safe to cast a class to a derived class that just adds additional functions?
body: <p>I have a class that contains a lot a data. Depending on the situation I need to output this data in different ways. I want the output routines for each of those situations separated and I want to keep the base class clean of that.</p>
  <p>Is it absolutely safe to cast to a derived class that does NOT add any data members but just non-virtual functions?</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  class Base {
  public:
    Base() { printf(&quot;Base()\n&quot;); }
    
    double A = 3.14;
    int B = 5;
    int C = 42;
  };
  
  class Derived1 : public Base {
  public:
    Derived1() { printf(&quot;Derived1()\n&quot;); }
    
    void DoStuff() {
      // doing stuff with base class's data
      printf(&quot;  A = %.4f\n&quot;, A);
      printf(&quot;  B = %d\n&quot;, B);
      printf(&quot;  C = %d\n&quot;, C);
    }
  };
  
  class Derived2 : public Base {
  public:
    Derived2() { printf(&quot;Derived2()\n&quot;); }
  
    void DoStuff() {
      // doing stuff with base class's data in a slightly different way
      printf(&quot;  A = %.4f, B = %d, C = %d\n&quot;, A, B, C);
    }
  };
  
  class OtherBase {
  public:
    OtherBase() { printf(&quot;OtherBase()\n&quot;); }
    int D = 10;
  };
  
  class Derived3 : virtual public OtherBase, virtual public Base {
  public:
    Derived3() { printf(&quot;Derived3()\n&quot;); }
    
    void SomeFunction() {
      A = 6.28;
      B = 1;
      C = 500;
      printf(&quot;Done some other stuff.\n&quot;);
    }
  };
  
  int main() {
    printf(&quot;Base class\n\n&quot;);
    Base* Instance = new Base();
    printf(&quot;Cast to Derived1:\n&quot;);
    static_cast&lt;Derived1*&gt;(Instance)-&gt;DoStuff();
    printf(&quot;Cast to Derived2:\n&quot;);
    static_cast&lt;Derived2*&gt;(Instance)-&gt;DoStuff();
    
    printf(&quot;\nOther class derived from base class\n\n&quot;);
    Derived3* Instance2 = new Derived3();
    Instance2-&gt;SomeFunction();
    printf(&quot;Cast to Derived1:\n&quot;);
    static_cast&lt;Derived1*&gt;(static_cast&lt;Base*&gt;(Instance2))-&gt;DoStuff();
    printf(&quot;Cast to Derived2:\n&quot;);
    static_cast&lt;Derived2*&gt;(static_cast&lt;Base*&gt;(Instance2))-&gt;DoStuff();
    printf(&quot;c-style cast to Derived1:\n&quot;);
    ((Derived1*) Instance2)-&gt;DoStuff();
    printf(&quot;c-style cast to Derived2:\n&quot;);
    ((Derived2*) Instance2)-&gt;DoStuff();
    return 0;  
  }
  </code></pre>
  <p>The part where class &quot;Derived3&quot; (that has virtual inheritance of &quot;Base&quot;) is cast to &quot;Base&quot; first and then to &quot;Derived1&quot;/&quot;Derived2&quot; does not look very good to me but I know how c-style casts are frowned upon in the c++ community. In this small example however, the c-style cast are compiling and working nicely:</p>
  <p>I know there is the issue with multiple inheritance where a cast to one of the base classes can have a different pointer address then the inherited class it was cast from. So I understand that it might be too dangerous to rely on it.</p>
  <p>Are the static casts I did in the example at the top safe?</p>