Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest answer to your question is that you must declare your container correctly <strong>and</strong> you must define an appropriate assigment operator for your class. Working as closely as possible from your example:</p> <pre><code>typedef class MyActionableClass T; T* getGlobalPointer(); void AddInstance(T const&amp; objInstance) { T* arrayFromElsewhere = getGlobalPointer(); //ok, now at this point we have a reference to an object instance //and a pointer which we assume is at the base of an array of T **objects** //whose first element we don't mind losing //**copy** the instance we've received arrayFromElsewhere[0] = objInstance; //now invoke the action() method on our **copy** arrayFromElsewhere[0].action(); </code></pre> <p>}</p> <p>Note the signature change to <em>const reference</em> which emphasizes that we are going to copy the original object and not change it in any way.</p> <p>Also <strong>note carefully</strong> that arrayFromElsewhere[0].action() <strong>is NOT</strong> the same as objInstance.action() because you have made a copy — action() is being invoked in a different context, no matter how similar.</p> <p>While it is obvious you have condensed, the condensation makes the reason for doing this much less obvious — specifying, for instance, that you want to maintain an array of callback objects would make a better case for “needing” this capability. It is also a poor choice to use “T” like you did because this tends to imply template usage to most experienced C++ programmers.</p> <p>The thing that is most likely causing your “unexplained” crash is that assignment operator; if you don't define one the compiler will automatically generate one that works as a bitwise copy — almost certainly <strong>not</strong> what you want if your class is anything other than a collection of simple data types (POD).</p> <p>For this to work properly on a class of any complexity you will likely need to define a deep copy or use reference counting; in C++ it is almost always a poor choice to let the compiler create any of ctor, dtor, or assignment for you.</p> <p>And, of course, it would be a good idea to use standard containers rather than the simple array mechanism you implied by your example. In that case you should probably also define a default ctor, a virtual dtor, and a copy ctor because of the assumptions made by containers and algorithms.</p> <p>If, in fact, you do <strong>not</strong> want to create a copy of your object but want, instead, to invoke action() on the <em>original</em> object but from within an array, then you will need an array of pointers instead. Again working closely to your original example:</p> <pre><code>typedef class MyActionableClass T; T** getGlobalPointer(); void AddInstance(T&amp; objInstance) { T** arrayFromElsewhere = getGlobalPointer(); //ok, now at this point we have a reference to an object instance //and a pointer which we assume is at the base of an array of T **pointers** //whose first element we don't mind losing //**reference** the instance we've received by saving its address arrayFromElsewhere[0] = &amp;objInstance; //now invoke the action() method on **the original instance** arrayFromElsewhere[0]-&gt;action(); </code></pre> <p>}</p> <p>Note closely that arrayFromElsewhere is now an array of <strong>pointers to objects</strong> instead of an array of actual objects.</p> <p>Note that I dropped the const modifier in this case because I don’t know if action() is a const method — with a name like that I am assuming not…</p> <p>Note carefully the ampersand (address-of) operator being used in the assignment.</p> <p>Note also the new syntax for invoking the action() method by using the pointer-to operator.</p> <p>Finally be advised that using standard containers of pointers is fraught with memory-leak peril, but typically not <em>nearly</em> as dangerous as using naked arrays :-/</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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