Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, I will try to explain in a simple way to make it easier for a beginner to understand better.</p> <ol> <li><p>Yes, it's the same as dot, but it's used for pointers.</p> <pre><code>MyClass a; a.myMethod(); MyClass* b = new MyClass; b-&gt;myMethod(); delete b; </code></pre> <p>Both a and b call the same method, but a is allocated in the heap and b is a pointer allocated by new. Memory explicity allocated should always be deallocated (thus the delete b statement). This is actually a simplification, what the arrow does is dereference the pointer and call the dot operator over it. If this makes your head spin at the moment, just take the simple explanation for now, it will sink later as you became more acostumated with the language.</p></li> <li><p>Header files are not obligatory, but they are usefull to reference what was declared in translation unit in another one. So, for example, you declare MyClass in a header, than you can use it in both myfilea.cpp and myfileb.cpp without too much effort having to declare it more than once.</p> <p>To properly understand the header files you need to know that when you use statements like:</p> <pre><code>#include "myheader.h" </code></pre> <p>What the compiler's preprocessor does is copy and paste all the contents of myheader.h into the file that included it. Since the compiler needs to know about the types (usually classes) before letting you use them, in each file you need to "redeclare what the types are". It's entierely possible to implement everything inside the header file actually, similar to what would look like a Java code, then in one main.cpp file include those files, but then the code would take a lot longer to compile and would be harder to follow.</p></li> </ol>
 

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