lundi 14 octobre 2019

Design for mixed structure in c++?

I have an object A, some fields in A I want them to be initiated at server start time only once, say A.a, A.b. some fields in A I want them to be initiated in dynamic memory since it's different per request. I also have a bunch of objects B,C,D.. that has the same fields as A but with different values, most of them are the same as A, so I want to use inheritance,

The design I can think of: separate A into 2 parts, StaticA and A, and then

class A {
 public:
  StaticA* static_data();
  // other getters fo A.c, A.d...
 private:
  Tc c_;
  Td d_;
}

Then static A will be like

StaticA {
  public:
   //getter for A.a, A.b
  private:
   Ta a_;
   Tb b_;
}

Then since I want to use inheritance, the code becomes ugly since I need to have a base class for StaticA/B/C/D and also A/B/C/D, also I want to hide these details from the clients, so I will need to write redundant getter functions in A's class.

Is this right way to design? Is there other better way to implement such mix logic? Thanks!

Aucun commentaire:

Enregistrer un commentaire