Note that there are some explanatory texts on larger screens.

plurals
  1. POKeep a central list of subclasses but avoiding static instances
    primarykey
    data
    text
    <p>I have a slightly nasty setup which I am trying to figure out a good way to overhaul.</p> <p>I have a <code>class Fractal</code> with a couple of pure virtual functions to do work. Each instance also has a human-readable name. I want to build a menu of all these subclasses so the user can switch between them - but, being lazy, I want to avoid having to both define each instance in its source file and list them all again in another. In other words, I want to build up this list of subclasses dynamically at runtime.</p> <p>What I have done (and have working) so far is:</p> <ul> <li>define a <code>class FractalRegistry</code> which is a singleton encapsulation of a <code>std::map&lt;std::string,Fractal*&gt;</code> (where the keys are the instance names), </li> <li>have the base <code>Fractal</code> constructor register each instance by name with that registry (and for completeness the base <code>~Fractal</code> deregister them), </li> <li>statically instantiate each base class once (with its name, which is a constructor argument).</li> </ul> <p>So for each new fractal I write something like this (paraphrased):</p> <pre><code>class SomeFractal : public Fractal { public: SomeFractal(std::string name, std::string desc) : Fractal(name,desc) {} virtual void work_function(...) { ... } } SomeFractal sf_instance("Some fractal", "This is some fractal or other"); </code></pre> <p>and the instance is added to the central list by the base class constructor, so I don't need to list it myself.</p> <p>However, this leads to having a load of static instances lying around, which appear to vanish if I move this code into a library. (Yes I can add a horrid empty function to each compile unit so I can force its inclusion, or resort to linker trickery such as <code>-Wl,--whole-archive</code>, but these don't seem like the right answer either.)</p> <p><em>Is</em> there a better way? What I guess I'm looking for is a way to write these Fractal implementations - all of which have the same interface, so I thought subclasses of a base class would be ideal - and to keep and populate this central registry of them but without leaving myself the landmine of static instances. </p> <p>What have I missed? I should state that I've been working with C for years but don't really have the zen of C++, so there might well be something that would do the job staring me in the face... (If I was writing this in C I'd think about writing a second-order macro composite which both declared some function pointers and populated a table with them and the fractals' names and descriptions, but that's an even more Cish thing to do and it really doesn't seem right for C++.)</p> <p><strong>Edit:</strong> What I am hoping to achieve is an elegant way of rearranging my code which makes it easy to add new fractal types and automatically populates a central list of them, but doesn't force the programmer to create a static instance of every fractal.</p>
    singulars
    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.
    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