Thursday 15 July 2010

C++ Would using an "initializer" class inside a constructor be appropriate/sensible? -


I have a class with several derivative classes, each with its own creator.

  square A {public: static A * create_new_A (int child_ID); Member_data; Zero method (); }; Class A_child_0: Public A {A_child_0 (int child_ID); // Luggage}; Class A_child_1: Public A {A_child_1 (int child_ID); // Luggage}; Etc ...   

and they are called something like in the main file:

  a ** array; Array = new A * [nchildren]; For (int i = 0; i  Create_new_A (i); }   

Where A is a factory that looks like this:

  A * A :: create_NU_A (Intrib_ID) {if (child_ID == 0 ) {Return a new A_child_0 (child_ID); } If (child_ID == 1) {return a new A_child_1 (child_ID); Now when manufacturer A_child_n is called, now I have a bunch of procedural order within the initiality for member_data values, which is a bit different in derivative classes, for example,   >   A_child_0 (int child_ID) {member_data.vector1 [0] = 0; For (i = 0; i & lt; 200; i ++) member_data.ct2 [0] = i; Read_in_conf_file ("conf.txt") // etc ... .a_child_1 (int child_id) {member_data.vector1 [0] = 0; For (i = 0; i & lt; 50; i ++) member_data.ct2 [0] = i * i; Read_in_conf_file ("conf.txt") // etc ....}   

What I'm curious about is a better way to go about this. Right now I'm thinking of preparing several these processal steps, so that it

  A_child_0 (int child_ID) {initializer_class * intializer; Initializer-> Start (child_id); } A_child_1 (int child_ID) {initializer_class * intializer; Initializer-> Start (child_id); }   

and is a class initiator () that implements the initial steps for different classes. I think this strategy is inspired by pattern. Does anyone have any suggestions on whether this approach is appropriate or sensible? Or does it just give up the problem for the early class and introduces an additional layer of complexity, if doing something like this is a bad idea, then what to do to try to avoid the constellator being a long program Could?

EDIT: As some said that the factory was incorrectly specified. It is still not really polished in the example I gave it, but it is not in the form of a mark as it was before.

Do you have to start it to fix this?

  struct base {std :: vector & lt; Int & gt; Information; Int minDefaultSize; Base (Conf. Int default size = 50): MinimumFeasure size (data size), data (Study :: Vector & lt; int & gt; (defaultSize, 0)) {} Virtual Zero Start () (for (int i = 0; i & lt; data.size (); ++ i) {data [i] = i;}} ~ virtual base () {};}; structure d1: base {d1 (): base (100 } {} Virtual zeros start () (for (int i = 0; i & lt; data; size (); ++ i) {data [i] = i * i;}}}; std :: shared_ptr & LT; Base & gt; Make (const int type) {type% 2 == 0? Return std :: shared_ptr  (new base ()): return std :: shared_ptr  gt; ; (New D1 ());} Int main () {std :: Vector & lt; std :: shared_ptr & lt; base for gt; & gt; for polyType; (int i = 0; i  Start ();}}    

No comments:

Post a Comment