Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Generic programming</strong> means that you are not writing source code that is compiled as-is but that you write "templates" of source codes that the compiler in the process of compilation transforms into source codes. The simplest example for generic programming are <strong>container classes</strong> like arrays, lists or maps that contain a collection of other objects. But there's much more to generic programming. In the context of C++ (and called <strong>meta programming</strong>) it means to write programs that are evaluated at <em>compile time</em>.</p> <p>A basic example of generic programming are templates of containers: In a statically typed language like C++ you would have to declare separate containers that hold integers, floats, and other types or deal with pointers to <code>void</code> and therefore losing all type information. Templates which are the C++ way of generic programming leverage this constraint by letting you define classes where one or more parameters are unspecified at the time you define the class. When you instance the template later you tell the compiler which type it should use to create the class out of the template. Example:</p> <pre><code>template&lt;typename T&gt; class MyContainer { // Container that deals with an arbitrary type T }; void main() { // Make MyContainer take just ints. MyContainer&lt;int&gt; intContainer; } </code></pre> <p>Templates are <strong>generic</strong> because the compiler translates the template into actual code. Note that in the case you don't instantiate your template no code will be generated for it <em>at all</em>. On the other hand, if you declare a <code>MyContainer&lt;int&gt;</code>, a <code>MyContainer&lt;float&gt;</code>, and a <code>MyContainer&lt;String&gt;</code> the compiler will create <strong>three</strong> versions of your code each of them having a different type. There will be some optimizations involved but basically your template code will be instantianted to three new types.</p> <p><strong>Iterators</strong> are a <strong>design pattern</strong> that were popularized in the seminal book "Design Patterns" by Gamma et al. It's a pattern to iterate over the content of a container class. Unlike using a <code>for</code>-loop an iterator is an instance of a class that points to a member of the container and gives you an unified interface to traverse the container as well as accessing the members. Take look at this example:</p> <pre><code>// Instanciate template QList with type int QList&lt;int&gt; myList; // put some ints into myList // Copyconstruct iterator that points to the // first member of the list. QList&lt;int&gt;::iterator i = myList.begin(); // Iterate through the list while (i != myList.end()) { std::cout &lt;&lt; *i &lt;&lt; std::endl; i++; } </code></pre> <p>In this C++ example I'm instantating a <strong>template</strong> QList with type <code>int</code>. QList a <strong>container</strong> class that stores a list of objects. In this example we will use it to store integers. </p> <p>Then I create an <strong>iterator</strong> <code>i</code> to traverse through the list. <code>myList.begin()</code> returns an iterator that points to the first element of the list. We can compare the iterator with another iterator <code>myList.end()</code> that points <em>after</em> the last element of the list. If both iterators are the same we know that we have passed the last elment. In the loop we're printing the element by accessing it with <code>*i</code> and go to the next element with <code>i++</code>. </p> <p>Note that in this example <code>*</code> and <code>++</code> are overloaded operators and reimplemented by the iterator class. In a programming language without operator overloading there could be methods like <code>i.element()</code> or <code>i.next()</code> that do the same task. It's important to see that <code>i</code> is not a pointer but a whole class that just mimics the behaviour of a pointer. </p> <p>What's the benefit of iterators? They provide a unified way to access the members of a container class, completely indepented on how the container class is implemented internally. No matter if your want to traverse a list, map or tree, the iterator classes (should) always work the same way.</p>
 

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