Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure what you mean. Sounds like you store the objects by value, and you you have an array of <code>Base</code>. That won't work, because as soon as you assign a Derived, that object will be converted to a Base, and the Derived part of the object is sliced away. But i think you want to have a array of pointers to base:</p> <pre><code>Base * bases[NUM_ITEMS]; for(int i=0; i&lt;NUM_ITEMS; i++) { int r = get_random_integer(); if(r == 0) bases[i] = new Derived1; else if(r == 1) bases[i] = new Derived2; // ... } </code></pre> <p>If you ever haved worked with pointers, you will know it's a pain in the ass to manage them, espacially pass around and not lose them, since you will need to call delete on them to free the memory and call the destructor of the objects. You can use shared_ptr, and it will manage that for you:</p> <pre><code>shared_ptr&lt;Base&gt; bases[NUM_ITEMS]; for(int i=0; i&lt;NUM_ITEMS; i++) { int r = get_random_integer(); if(r == 0) bases[i].reset(new Derived1); else if(r == 1) bases[i].reset(new Derived2); // ... } </code></pre> <p>Now, you can pass <code>bases[x]</code> to another shared_ptr, and it will note you have got more than one reference - it will call automatically delete if the last reference to the objects go out of scope. Ideally, you would also replace the raw array by std::vector:</p> <pre><code>std::vector&lt; shared_ptr&lt;Base&gt; &gt; bases; for(int i=0; i&lt;NUM_ITEMS; i++) { int r = get_random_integer(); if(r == 0) bases.push_back(shared_ptr&lt;Base&gt;(new Derived1)); else if(r == 1) bases.push_back(shared_ptr&lt;Base&gt;(new Derived2)); // ... } </code></pre> <p>Then you can pass the vector around, and don't lose the size of it, and you can dynamically add items to it on demand. Get the size of the vector using <code>bases.size()</code>. Read about <code>shared_ptr</code> <a href="http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/shared_ptr.htm" rel="nofollow noreferrer">here</a>.</p> <p>Conversion from a Base class to a Derived class should <em>only be done when absolutely necessary</em>. Normally, you want to use a technique called <code>polymorphism</code>, which means you call a function on the base pointer, but it will actually call a function defined in the derived class, having the same signature (name and parameters are the same type) and is said to <code>override</code> it. Read the <a href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming" rel="nofollow noreferrer">article</a> on wikipedia about it. If you really need to cast, you can do it like this for a raw pointer:</p> <pre><code>Derived1 * d = &amp;dynamic_cast&lt;Derived1&amp;&gt;(*bases[x]); </code></pre> <p>Using dynamic_cast ensures, that when you cast to the wrong type (i.e the type you cast is not the type that was created and assigned to the base pointer), you get an exception thrown by the operator. For the shared_ptr case, there are ways too:</p> <pre><code>shared_ptr&lt;Derived1&gt; d = dynamic_pointer_cast&lt;Derived1&gt;(bases[x]); if(d) { // conversion successful, it pointed to a derived. d and bases[x] point still // to the same object, thus share it. } </code></pre>
    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.
 

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