Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I strongly suggest you update your knowledge of pointers in C++. There is almost no reason to use raw pointers these days.</p> <p>To answer the question directly, the <code>Game.GetScreen()</code> function would want to 'const' differently depending on how exactly you wish for it behave.</p> <p>If you return a <code>const Screen*</code> you are returning a pointer to a Screen object that is constant (cannot be modified). You would normally wish to make such a function const allowing it to be called on a Game object that is const it self. If somebody has a non-const Game object, you can allow them to get the Screen object that they can then modify by simply returning a <code>Screen*</code>. </p> <p>That said, I return to my opening point, you shouldn't use raw pointers.</p> <p>Initially, you should just have the Screen object stored by value, and return references, giving you a class along the lines of:</p> <pre><code>Class Game{ Screen screen; public: const Screen&amp; GetScreen() const{ return screen; } Screen&amp; GetScreen(){ return screen; } }; </code></pre> <p>This may look like you are copying the Screen object to return it, but the return types ensure you are returning a reference to the same screen object, rather than a copy.</p> <p>If you really do need dynamic creation of the Screen object (ie, you cannot create it either before or as you create the Game object) then you should use a <code>std::unique_ptr&lt;Screen&gt;</code>. This can be used very much like a raw pointer, but will take care of a lot of the functionality for you. However, you wish to share access to this Screen pointer, so you probably want to use <code>std::shared_ptr&lt;Screen&gt;</code> and return <code>std::weak_ptr&lt;Screen&gt;</code> from the the get function. The weak_ptr can be used to allow others access, but your initial Game object still <em>owns</em> the Screen object.</p> <p>Long story short, store the Screen object by value, and return references to it.</p>
    singulars
    1. This table or related slice is empty.
    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. 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