Note that there are some explanatory texts on larger screens.

plurals
  1. POEffectively changing constness of member variable in subclass
    text
    copied!<p>While developing a game I have designed a class that in normal operation has a const member variable like so:</p> <pre><code>class Foo { /* ... */ protected: const boost::unordered_map&lt;short, short&gt; m_map; }; </code></pre> <p>However, in the accompanying game editor I would like to reuse the Foo class, but provide a way to edit this const member as it is used in rendering the game (and I would like to reuse the renderer for the editor). As such, I was planning on subclassing the Foo class in order to provide methods for changing various properties as well as serializing the data for saving the editor data. I'm wondering if there's an elegant way to do this while keeping the member as const or if there is a better design? </p> <p>The options I have come up with so far are:</p> <ul> <li>Remove the const keyword and simply provide no public interface for changing the variable</li> <li>Change the renderer to use an interface and implement it separately in both Foo and the editor version of Foo</li> <li>Reimplement the public interface of Foo to use a new, mutable map for the editor version of Foo</li> <li>Something else entirely?</li> </ul> <p>At first hand the interface path seems like the cleanest from a language perspective, but leads to an interface (abstract base class) for each and every class involved which is a decent effort. Suggestions and ideas welcome. (Note: I had a hard time coming up with a descriptive title; suggestions welcome for that also.)</p>
 

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