Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are missing the <code>virtual</code> keyword. In C++ methods are not virtual by default like they are in some other programming languages (e.g., Java). Try the following instead:</p> <pre><code>#include &lt;iostream&gt; class User { static const int min_pass_len = 10; public: virtual ~User() {} virtual int getPasswordLength() const { return min_pass_len; } }; class AdminUser: public User { static const int min_pass_len = 42; public: virtual ~AdminUser() {} virtual int getPasswordLength() const { return min_pass_len; } }; int main() { AdminUser a; User u, *p = &amp;a; std::cout &lt;&lt; u.getPasswordLength() &lt;&lt; "\n" &lt;&lt; a.getPasswordLength() &lt;&lt; "\n" &lt;&lt; p-&gt;getPasswordLength() &lt;&lt; "\n" &lt;&lt; std::endl; return 0; } </code></pre> <p>If you want to get rid of the method altogether, then either use the example of the <a href="https://stackoverflow.com/questions/4481285/how-to-store-a-magic-number-for-each-derived-class-that-is-then-accessible-from-t/4481363#4481363"><em>"curiously reoccuring template pattern"</em> mentioned by Sanjit</a> or externalize the constant instead embedding it within the class. I was thinking of something similar to how <code>std::numeric_limits</code> is implemented:</p> <pre><code>template &lt;typename T&gt; struct UserTraits { static const int minimum_password_length = 0; }; template&lt;&gt; struct UserTraits&lt;User&gt; { static const int minimum_password_length = 10; }; template&lt;&gt; struct UserTraits&lt;AdminUser&gt; { static const int minimum_password_length = 10; }; int main() { std::cout &lt;&lt; UserTraits&lt;User&gt;::minimum_password_length &lt;&lt; "\n" &lt;&lt; UserTraits&lt;AdminUser&gt;::minimum_password_length &lt;&lt; "\n" &lt;&lt; std::endl; return 0; } </code></pre> <p>I usually prefer the first approach since it works when you have a pointer to an instance. The latter method isn't nearly as useful since you cannot use it to extract the constant from an instance without resorting to writing a free function and even then you loose the dynamic typed aspect.</p> <pre><code>template &lt;typename T&gt; int getPasswordLength(T*) { return UserTraits&lt;T&gt;::minimum_password_length; } template &lt;typename T&gt; int getPasswordLength(T&amp;) { return UserTraits&lt;T&gt;::minimum_password_length; } </code></pre> <p>The problem with this approach is that you would get the wrong result if you called <code>getPasswordLength(p)</code> using <code>p</code> in the first snippet.</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. VO
      singulars
      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