Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ vector push_back with pointers
    primarykey
    data
    text
    <p>I'm trying to get my head around solving the following problem. I have the following function which accepts a const reference to an object. What I want to do, is give storage, a pointer to the object component is referencing. However, when I call storage.push_back() changes the object component is referencing (it's member data gets corrupted)</p> <p>I'm just trying to store a pointer to the same object component is referencing, not alter the object at all. </p> <pre><code>void InputSystem::RegisterInputAcceptingEntity(const PlayerInputComponent &amp;component) { auto *cpy = &amp;component; std::vector&lt;const PlayerInputComponent*&gt; storage; storage.push_back(cpy); } </code></pre> <p>Why would the above code at all change the object component is referencing?</p> <p>edit: Ok I edited the vector to contain constant pointers.</p> <p>Here are some screenshots showing exactly what is happening. Here in the callee, if we inspect the argument that is going to be passed by reference, we can see the input_component member is not corrupted. <img src="https://i.stack.imgur.com/bdLAk.png" alt="enter image description here"> url to image: <a href="https://i.imgur.com/lKpRm.png" rel="nofollow noreferrer">http://i.imgur.com/lKpRm.png</a></p> <p>However, in the second image, just after stepping into the above said method:</p> <p><img src="https://i.stack.imgur.com/2jjke.png" alt="enter image description here"> url to image: <a href="https://i.imgur.com/x0Vdn.png" rel="nofollow noreferrer">http://i.imgur.com/x0Vdn.png</a></p> <p>I've been under the impression that passing by const reference, there should be no way for the objects to differ inside/outside the function. I don't see why the object would be altered at all, if passed by const reference.</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.
 

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