Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think <a href="https://stackoverflow.com/questions/5128475/managing-diverse-classes-with-a-central-manager-without-rtti/5128502#5128502">bdonlan's suggestion</a> is good, but requiring each module type to declare a distinct <code>INTERFACE_ID</code> is a maintenance headache. The distinctness can be accomplished automatically by having each module type declare a static object and using its address as the ID:</p> <pre><code>struct IModuleFoo : public IModuleBase { static char distinct_; // Exists only to occupy a unique address static const void *INTERFACE_ID; virtual void foo() = 0; }; // static members need separate out-of-class definitions char IModuleFoo::distinct_; const void *IModuleFoo::INTERFACE_ID = &amp;distinct_; </code></pre> <p>In this case we are using <code>void *</code> as the interface ID type, instead of <code>int</code> or an enumerated type, so the types in some other declarations will need to change.</p> <p>Also, due to quirks in C++, the <code>INTERFACE_ID</code> values, despite being labelled <code>const</code>, are not "constant enough" to be used for <code>case</code> labels in <code>switch</code> statements (or array size declarations, or a handful of other places), so you would need to change the <code>switch</code> statement to an <code>if</code>. As described in section 5.19 of the standard, a <code>case</code> label requires an <em>integral constant-expression</em>, which roughly speaking is something the compiler can determine just from looking at the current translation unit; while <code>INTERFACE_ID</code> is a mere <em>constant-expression</em>, whose value cannot be determined until link time.</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.
    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