c++ - Pointer to array Maintain counter of elements -


i have interface multiple classes inheritance.

    class someinterface     {         virtual void somemethod() = 0;     }     class : public someinterface     {         public:             void somemethod()             {                 //do             }     }      class b : public someinterface     {         public:             void somemethod()             {                 //do             }     }      class c : public someinterface     {         public:             void somemethod()             {                 //do             }     } 

for each of classes a, b, c have created array different sizes of actual type inside container class.

    class acontainer      {         public:             as[10];     }      class bcontainer      {         public:             b bs[5];     }     etc... 

furthermore have array of pointers "someinterface", want have pointer each of actual arrays this.

    #define someinterrface_size 3     someinterface *array[someinterrface_size];     array[0] = acontainer.as; //could &acontainer.as[0]     array[1] = bcontainer.bs;     array[2] = ccontainer.cs;      (int = 0; < someinterrface_size; ++i)     {         int elements = //here need solution size                        //so can iterate through array, pointer points to.         (int = 0; < elements; ++i)         {             //call interface method on each element.         }     } 

the problem occurs, when have use someinterface array, since isn't possible size of actual array through someinterface pointer..

what solution problem? need solve this. don't want use dynamic allocation, no solution vector<> or malloc etc. because i'm writing arduino.

it won't work. in c++ have know size of elements in array. a, b, , c might different sizes, can't treat arrays of them same.

&acontainer.as[i] == &acontainer.as + * sizeof(a) 

but

&bcontainer.bs[i] == &bcontainer.bs + * sizeof(b) 

so it's impossible same machine code iterate on arrays of a , of b. if want iterate on array of objects, need know exact type.

remember in c++, if want polymorphic virtual call, need go through pointer or reference. solution copy pointers elements in each array 1 "master" array.

someinterface *ptrs[num_a + num_b + num_c]; someinterface **dest = ptrs; (int = 0; < num_a; ++i) {     *dest++ = &acontainer.as[i]; } (int = 0; < num_b; ++i) {     *dest++ = &bcontainer.bs[i]; } // et cetera... 

this uses little bit of space because you're storing pointers, not actual objects.

edit: guess if want save space:

someinterface *arrays[] = { acontainer.as, bcontainer.bs, ccontainer.cs }; int objsizes[] = { sizeof(a), sizeof(b), sizeof(c) }; int arrlengths[] = { num_a, num_b, num_c };  (int j = 0; j < sizeof(arrays)/sizeof(arrays[0]); ++j) {     void *ptr = arrays[j];     (int = 0; < arrlengths[j]; ++i) {         someinterface *iptr = (someinterface *)ptr;         iptr->method();         ptr += objsizes[j];     } } 

(this untested, might need tweak little.)

in theory since arrays full of compile-time constants, should optimize out fast. if doesn't, code run slower because incrementing pointers value known @ runtime instead of compile time. should check assembly output if care speed.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -