c++ - How to make data available to all objects of a class? -


this basic somehow cannot figure out.

say have class a embeds 42 things, plus common data:

class {   thing things[42];   int common_data[1024]; } 

i each thing have access common data, don't want copy data in each thing object, nor pay price of pointer in each thing. in other word, thing this:

class thing {   int ident;   int f() {     return common_data[ident];   } } 

of course here common_data unbound. canonical way make work?

fwiw working subset of c++ no dynamic allocation (no "new", no inheritance, it's c nice syntax call methods , declare objects); ideally looking solution fits in subset.

you can solve issue making common_data attribute of class static. static variables shared members of class a, , accessible if make public.

class  {   private:     thing things[42];   public:     static int common_data[1024]; } 

it can accessed doing...

a::common_data[index]; 

Comments