Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I typically prefer to use pointer when the object is large and to use a stack object when it is small. Pointers allow for some things such as lazy init and forward declaration, while stack objects reduce the likely hood you'll need to create your own copy constructor and having to worry about explicitly destroying the object in the destructor. (although, you could use a shared pointer type object to do this for you) Also, a stack object is typically faster to access, but in most scenarios this shouldn't be a reason for choosing one over the other as the difference in most applications is negligible.</p> <p>In reference to your code, you typically would return a <code>const Investment &amp;</code> in order to not allow direct modifications to Investment object. As mentioned above, if you use a raw pointer, you should create a copy constructor and a destructor that properly manage the memory on the object. With a pointer object you would typically return a <code>const Investment *</code> again to remove the ability for an outside caller to directly modify your object.</p> <p>Take notice of how your functions are declared in the Investment class when you return const correct Investment objects. If you simply have <code>Investment::getNumShares()</code>, you will not be able to access the function using a const reference or const pointer. To fix this issue, mark all member functions that do not modify the object to be const such as <code>Investment::getNumShares() const</code></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. 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.
    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