Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling class method without using the object?
    primarykey
    data
    text
    <p>This might seem like a silly question to the more seasoned C++ coders out there, but I thought once you have a class and you create an object of that class, you call a public method of that class using the object you created (unless it is a static method, in which case you call it either using an object or the class name itself)?</p> <p>Then why does this work?</p> <p>Function definition:</p> <pre><code>template &lt;typename Object&gt; void printList(const List&lt;Object&gt;&amp; theList) { if (theList.isEmpty()) cout &lt;&lt; "Empty list!" &lt;&lt; endl; else { ListItr&lt;Object&gt; itr = theList.first(); for(; !itr.isPastEnd(); itr.advance()) cout &lt;&lt; itr.retrieve() &lt;&lt; " "; } cout &lt;&lt; endl; } </code></pre> <p>Function call:</p> <pre><code>printList(myList); </code></pre> <p>What am I missing here? How does the rest of the program know that <code>printList()</code> belongs to the class <code>List&lt;int&gt;</code> unless I call <code>printList()</code> using an object of <code>List&lt;int&gt;</code>?</p> <p>This works too by the way, I just checked. I would have used this way of calling and defining the function. Note that this time the function is defined using the <code>this</code> pointer, the way I would have thought it works.</p> <p>Function definition:</p> <pre><code>template &lt;typename Object&gt; void List&lt;Object&gt;::printList() { if(this-&gt;isEmpty()) cout &lt;&lt; "Empty list!" &lt;&lt; endl; else { ListItr&lt;Object&gt; itr = this-&gt;first(); for(; !itr.isPastEnd(); itr.advance()) cout &lt;&lt; itr.retrieve() &lt;&lt; " "; } cout &lt;&lt; endl; } </code></pre> <p>Function call:</p> <pre><code>myList.printList(); </code></pre>
    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.
 

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