Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride array size in subclass
    primarykey
    data
    text
    <p>I have an array as a member of a class. In a subclass, I would like to re-define the array with a different size. I want to do this because I anticipate making many subclasses, each with only the array size it needs, and nothing more. </p> <pre><code>class Foo { Foo() {ivar = 1}; int thisArray[2]; int ivar; } class Bar : public Foo { Bar() {ivar = 3}; int thisArray[4]; } int main() { Foo myFoo; Bar myBar; Foo fooCollection[] = {myFoo,myBar}; cout &lt;&lt; "myFoo array size = " &lt;&lt; sizeof(myFoo.thisArray)/sizeof(int) &lt;&lt; endl; cout &lt;&lt; "myBar array size = " &lt;&lt; sizeof(myBar.thisArray)/sizeof(int) &lt;&lt; endl; for (int n=0;n&lt;2;n++) { cout &lt;&lt; "fooCollection[" &lt;&lt; n &lt;&lt; "] array size = "; cout &lt;&lt; sizeof(fooCollection[n].thisArray)/sizeof(int) &lt;&lt; endl; } for (int n=0;n&lt;2;n++) { cout &lt;&lt; "fooCollection[" &lt;&lt; n &lt;&lt; "] ivar = "; cout &lt;&lt; fooCollection[n].ivar &lt;&lt; endl; } } </code></pre> <p>My results are:</p> <pre><code>myFoo array size = 2 myBar array size = 4 fooCollection[0] array size = 2 fooCollection[1] array size = 2 fooCollection[0] ivar = 1 fooCollection[1] ivar = 3 </code></pre> <p>I get that, since I declare the array objects as objects of class <code>Foo</code>, that referring to <code>myBar</code> within that scope would reference <code>myBar</code> as though it was a <code>Foo</code> and consequently interpret the size of <code>thisArray</code> as equivalent to 2. I also understand why <code>ivar</code> comes out the way it does. </p> <p>Is there a way to affect the size of <code>thisArray</code> within the <code>Bar</code> class so its "correct" size can be recognized within an array of <code>Foo</code> objects? I would use a vector, but they are not friendly on the arduino platform. I could also simply make the array within the Foo class with a size of 100, but I am trying to be conscious of memory allocation.</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