Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Virtual Methods for Class-Specific Attributes or External Structure
    primarykey
    data
    text
    <p>I have a set of classes which are all derived from a common base class. I want to use these classes polymorphically. The interface defines a set of getter methods whose return values are constant across a given derived class, but vary from one derived class to another. e.g.:</p> <pre><code>enum AVal { A_VAL_ONE, A_VAL_TWO, A_VAL_THREE }; enum BVal { B_VAL_ONE, B_VAL_TWO, B_VAL_THREE }; class Base { //... virtual AVal getAVal() const = 0; virtual BVal getBVal() const = 0; //... }; class One : public Base { //... AVal getAVal() const { return A_VAL_ONE }; BVal getBVal() const { return B_VAL_ONE }; //... }; class Two : public Base { //... AVal getAVal() const { return A_VAL_TWO }; BVal getBVal() const { return B_VAL_TWO }; //... }; </code></pre> <p>etc.</p> <p>Is this a common way of doing things? If performance is an important consideration, would I be better off pulling the attributes out into an external structure, e.g.:</p> <pre><code>struct Vals { AVal a_val; VBal b_val; }; </code></pre> <p>storing a <code>Vals*</code> in each instance, and rewriting <code>Base</code> as follows?</p> <pre><code>class Base { //... public: AVal getAVal() const { return _vals-&gt;a_val; }; BVal getBVal() const { return _vals-&gt;b_val; }; //... private: Vals* _vals; }; </code></pre> <p>Is the extra dereference essentially the same as the vtable lookup? What is the established idiom for this type of situation? Are <em>both</em> of these solutions <em>dumb?</em> Any insights are greatly appreciated</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.
    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