Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>The &amp; operator</strong></p> <p>Has 2 primary uses: one is to take the address of a variable in memory, as you seem to be aware. So if I have <code>int count = 0;</code> the value of <code>count</code> is 0, but where is this variable in memory? &amp;count will return this address, which can be useful in a variety of ways.</p> <p>The other use is to declare/define a reference to a type. Note that in this context <code>&amp;</code> is no longer an operator, and can not be given custom behavior in a user defined class. <code>int &amp; myRef</code> represents a reference to an integer, which is very much like a pointer, but instead of having a variable to hold some address in memory, you are using one variable as a sort of pseudonym for another. <code>int &amp;myRef = count;</code>, after this, <code>count</code> and <code>myRef</code> can be used 100% interchangeably. Note that this use only comes when <em>defining</em> or <em>declaring</em> a variable, never as the right hand side of an expression, or in other contexts. As you noted, whitespace is ignored for references, which is the same behavior as pointers.</p> <p><strong>const</strong></p> <p>It's always good behavior to use const when you can (in my opinion). It makes clear how variables will be used. Imagine if you have a function <code>void foo(int &amp; someIntRef)</code> or <code>void foo(int * someIntPtr)</code>. When I call foo, it is not guaranteed that the variable I pass in won't be changed. Maybe the integer I am passing in is something I am keeping track of very closely, and don't want anyone else to maybe or maybe not change it, so I would be forced to copy it to ensure the definition of <code>foo</code>, which I may not even have access to, doesn't alter the variable. In most situations, if I saw this function signature, I would <em>assume</em> the function would alter my variable. Also, there is a performance benefit if the compiler knows a function parameter is const, but as you are new to c++ it might be best to not worry about exactly how/why at this point.</p> <p><strong>-> operator</strong></p> <p>This is an operator (note, it may be overridden in custom classes, but it's very rare) that almost always access a data member or member function of a pointer to an object. If I have a struct:</p> <pre><code>struct Shape { int mSideCount; }; ... Shape SomeShape; Shape * ptrShape = &amp;SomeShape; </code></pre> <p>I can access the <code>mSideCount</code> data member of <code>SomeShape</code> with <code>SomeShape.mSideCount</code>. But what about ptrShape? It's not a Shape, it's a pointer (specifically, <code>Shape *</code>). I have to dereference it first, with the <code>*</code> operator, like <code>(*ptrShape).mSideCount</code>. It's kinda ugly. To make life easier, <code>-&gt;</code> exists to combine the dereference and member access: <code>ptrShape-&gt;mSideCount</code>.</p> <p><strong>new operator</strong></p> <p><code>new</code> allocates memory for an object, and ensures it is constructed correctly (calls an appropriate constructor for the object, which is a member function that looks like <code>Shape(){}</code> in the case of the Shape example above). The big difference is where the memory comes from. When you create a variable like <code>SomeShape</code> above, it is created in a section of memory called the <em>stack</em> (hence, stack overflow). The stack usually has a relatively small maximum size, and any variables declared on the stack go away once the block (inside function brackets, loop brackets, whatever the block is) ends, so can't be use in other functions easily. <code>new</code> on the other hand by default creates the object using memory on the <em>heap</em>, which is more closely "all the memory on your system". There are books written about memory management and architecture alone, so this is an extremely brief introduction.</p> <p>As many others have noted - these are fairly basic c++ concepts. You will be back here soon when you see other even less common language features. Get a good book, read through it, understand everything it says, do some of the examples, and then come here to fill in gaps in your knowledge that you still can't wrap your head around. I personally like c++ Dietle and Dietle, but anything will help you.</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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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