Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ How to I replace this if...else statement?
    primarykey
    data
    text
    <p>I have the following C++ code (simplified version):</p> <pre><code>class Shape { bool isCircle = false; bool isSquare = false; } class Circle : public Shape { // some special members/methods } class Square : public Shape { // some special members/methods } class CAD { virtual DrawCircle(Circle * circle) = 0; } class SWX : public CAD { virtual DrawCircle(Circle * circle){// do some stuff that draws circle on SWX system} } class PRO : public CAD { virtual DrawCircle(Circle * circle){// do some stuff that draws circle on PRO system} } int main() { Circle * circle = new Circle(); circle-&gt;isCircle = true; Square * sq = new Square; sq-&gt;isSquare = true; vector&lt;Shape*&gt; shapes; shapes.push_back(circle); shapes.push_back(sq); SWX * swx = new SWX(); for( int i = 0 ; i &lt; shapes.size() ; ++i ) { if( shapes[i]-&gt;isCircle ) { SWX-&gt;DrawCircle((Circle*)(shapes[i])); } else if( shapes[i]-&gt;isSquare ) { SWX-&gt;DrawSquare((Square*)(shapes[i])); } } </code></pre> <p>I wish to remove the need for if...else (if at all possible within the constraints stated below). </p> <p>My constraints right now are:</p> <ul> <li>The CAD and derived classes are ginormous classes with various external dependencies. </li> <li>The CAD classes cannot be merged with the Shape and derived classes (that would have been ideal, since then I can use polymorphism to solve my problem), since other projects/classes depend on the Shape classes and cannot depend on the CAD classes.</li> <li>There are more than a dozen Shape-derived classes with a half dozen CAD-derived classes and this if...else is happening in numerous locations - so it would help if any solution is simple to understand (easier to convince my teammates to change legacy code).</li> </ul> <p>Any suggestions/comments/solution you have would be most welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload