Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In a statically typed language, such as C++ or Java for example, <code>static</code> may refer to the information known at compilation time while <code>dynamic</code> refers to the information known at runtime.</p> <p>For example:</p> <pre><code>struct Base { virtual std::string name() const { return "Base"; } }; struct Derived: Base { std::string name() const { return "Derived"; } }; void print(Base const&amp; b) { std::cout &lt;&lt; b.name() &lt;&lt; "\n"; } </code></pre> <p>In the <code>print</code> method, the <code>static</code> type of <code>b</code> is <code>Base const&amp;</code>. Therefore, the compiler will check that all methods called exist in the context of a <code>Base</code> object.</p> <p>However, when execution comes, the call to <code>name</code>, as the method is virtual, is performed with regard to the <code>dynamic</code> type of the object:</p> <ul> <li>this may be <code>Base</code></li> <li>this may be <code>Derived</code></li> <li>this may be another derived class from <code>Base</code> that we know not yet</li> </ul> <p>Therefore, in the following example:</p> <pre><code>int main(int argc, char* argv[]) { if (argc == 1) { Base base; print(); } else { Derived derived; print(derived); } }; </code></pre> <ul> <li>The <code>static</code> and <code>dynamic</code> type of <code>base</code> is <code>Base</code> and <code>derived</code> is <code>Derived</code>.</li> <li>In the <code>print</code> method, the <code>static</code> type of <code>b</code> is <code>Base</code> (always)</li> <li>Depending on the number of arguments, the <code>dynamic</code> of <code>b</code> is either <code>Base</code> or <code>Derived</code></li> </ul> <p>It is a current mistake to assume that polymorphism is necessarily based on dynamic memory allocation, but the two concepts, while not orthogonal, can be used without one another in some conditions.</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.
    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