c++ - How do I (elegantly) assign reference members according to a char alias? -


disclaimer

i don't propose apply design anywhere, i've been curious nonetheless how 1 implement in c++, in particular given c++'s lack of reflection. (i'm simultaneously learning , experimenting c++11 features, please use c++11 features helpful.)

the effect

what want achieve purely cosmetic.

i'd class binds arbitrary number of, say, vectors, using referenced members (which, understand, must initialized during construction), provides "aliases" accessing these vectors members.

to give minimal example, want work—

std::vector<int> one; std::vector<int> two; std::vector<int> three;  foo foo(std::make_pair('b', one),         std::make_pair('d', two),         std::make_pair('f', three));  foo.dosomething(); 

where

class foo { public:     // i'm using variadic templates here sake of learning,     // initializer lists work well.     template <typename ...tail>     foo(std::pair<char, std::vector<int>&> head, tail&&... tail) // : ???     {         // ???     }      virtual void dosomething()     {         d.push_back(42);         std::cout << d[0] << std::endl;     } private:     std::vector<int> a&, b&, c&, d&, e&, f&, g&; // , on... } 

and that

std::cout << one[0] << std::endl; // outputs 42 outside class... 

but refuse answer unless know why...

why want this? well, don't really want it, application had in mind this. suppose i'm building kind of data analysis tool, , have clients or operations people know basic logic , c++ syntax, don't understand oop or beyond cs 101. things go lot smoother if write own dosomething()s on fly, rather communicate every need developers. however, it's not realistic them set unix accounts, teach them how compile, , on. suppose instead i'd build intranet web interface lets them write body of dosomething() , configure datasets they'd "alias" uppercase char, , upon submission, generates c++ child class of foo overrides dosomething(), builds, runs, , returns output. (suspiciously specific "hypothetical," eh? :-) okay, situation exist in world—however inspired idea , desire explore it—i don't think it'd worth implementing.) obviously, whole uppercase char ordeal isn't absolutely necessary, it'd nice touch because datasets associated standard letters, e.g. p price, q quantity, etc.

the best can do...

to honest, can't figure out how i'd make work using references. prefer using references if possible, these reasons.

with pointers, guess i'd this—

class foo { public:     template <typename ...tail>     foo(std::pair<char, std::vector<int>*> head, tail&&... tail)     {         std::vector<int>[26] vectors = {a, b, c, d, e, f, g}; // , on...          // haven't learned how use std::forward yet, picture...         // , dear lord, forgive me i'm do...         vectors[tail.first - 65] = tail.second;     }      virtual void dosomething()     {         d->push_back(42);         std::cout << (*d)[0] << std::endl;     } private:     std::vector<int> a*, b*, c*, d*, e*, f*, g*; // , on... } 

but not elegant.

  1. is there way use references , achieve this?

  2. is there way make more generic, e.g. use pseudo-reflection methods avoid having list uppercase letters again?

  3. any suggestions on alternative designs preserve primary goal (the cosmetic aliasing i've described) in more elegant or compact way?

you may use like:

class foo { public:     template <typename ...ts>     foo(ts&&... ts)  : m({std::forward<ts>(ts)...}),     a(m.at('a')),     b(m.at('b'))     // , on...     {     }      virtual void dosomething()     {         a.push_back(42);         std::cout << a[0] << std::endl;     } private:     std::map<char, std::vector<int>&> m;     std::vector<int> &a, &b; //, &c, &d, &e, &f, &g; // , on... }; 

but requires each vector given, so

foo(std::vector<int> (&v)[26]) : a(v[0]), b(v[1]) // , on... { } 

or

foo(std::vector<int> &a, std::vector<int> &b /* , on */) : a(a), b(b) // , on... { } 

seems more appropriate.

live example.

and seems simpler if class foo owns vector, have

class foo { public:     virtual ~foo() {}     virtual void dosomething() { /* code */ }      std::vector<int>& geta() { return a; } private:     std::vector<int> a, b, c, d; // , on  }; 

and provide getters initialize internal vectors.

and then

std::vector<int>& 1 = foo.geta(); // or foo.a if let members public. 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -