A C++ issue with multiple inheritance, templates and static variables -


i have code similar following:

template<class objtype> class jsonable {  private:     static map<string, jsonelem> config;  protected:     virtual void setconfig() = 0;  //other fields , methods in public/private }  class user : public jsonable<user> {  protected:     virtual void setconfig();  //other fields , methods in public/private }  class client : user {  protected:     virtual void setconfig() {user::setconfig(); /* more config */}  //other fields , methods in public/private } 

the main idea of code save in static variables data related class referenced in template. problem comes when want inherit user class: static variable shared between user , client classes, instead of 1 static variable each class.

i've tried like:

class client : user, jsonable<client> 

but bunch of problems appeared (many methods same name, , other related inherit 2 times same class). don't know if there elegant way of this, or if there way @ all. (i'm bit newbie in c++)

any idea welcome! :). , of course, can "copy" contents of user client but... not until there no more options.

edit: in order add context , details question, i'm going explain bit i'm doing (or want do). jsonable class provides ability serialize json class (helped https://github.com/nlohmann/json).

to achive this, uses static map store each jsonable-field name , info (type , position relative start of class in memory, can serialized , deserialized).

the problem comes if class inherits class inherits jsonable. both shares map, baseclass data consider when serializing/deserializing. hope explanation helps understand...

edit2: giving full code in question seems overkilling me. if wants compile, i've uploaded git repo: https://github.com/handbe/jsontests people have put interest on question!.

a possible solution can derive client both user (because user) , jsonable<client> (private/public apart)

class user : public jsonable<user> {  protected:     virtual void setconfig();  //other fields , methods in public/private };  class client: public user, public jsonable<client> {    virtual void setconfig()    {        user::setconfig();        // more config, referred jsonable<client>::map    } } 

because has implement jsonable (regardless of user).

this so-called "stacked parallelogram" inhertiance pattern common in multiple interface implementations modular behavior.

now user , client have each own configuration


Comments