Friday 15 July 2011

c++ - Declare inherited templatized objects and push them back into vector - elegant way? -


After

I'm trying to declare a lot of templatized is & gt; t & gt; Legacy base from object, and std :: vector & lt; Base *> .

  struct base {...}; Template & lt; Typename T & gt; Structured derivative: base {/ * ctor (const string & amp;) * / ...} made & lt; Bool & gt; Online {"Online"}; Derivative & LT; Bool & gt; Official {"official"}; Derivative & LT; Bool & gt; NoRotation {"no_rotation"}; Derivative & LT; Bool & gt; NoBackground {"no_background"}; Derivative & LT; Integer & gt; Nosound {"no_sound"}; Derivative & LT; String & gt; NoMusic {"no_music"}; Derivative & LT; Bool & gt; BlackAndWhite {"black_and_white"}; Of vector & lt; Base * & gt; Configured value (& amp; Online, & amp; official, & amp; nautritation, & amp; NoBackground, & amp; No Sound, & No Music, & Black & White};   

As you can see, the code is terrible. There is no way to automate this without having vector passing as a constant & amp; < Code> Derivative & lt; t & gt; :: Derivative & lt; t & gt; (...) Constructor?

By automating I mean that the names of the objects are repeated Means to be avoided. T & gt; I want to do a std :: vector to fill all my derivative & lt. List all of them manually

"Text">

Therefore, the lifespan is one of the problems you face, and how this method is used in your program. Is another effect.

So you derived & lt; & Gt; Examples are not ownership of vector, you must ensure that they survive. There are three basic approaches, which can be added in some cases.

First: Create a class that stores and populates the vector. If you have the same type & lt; & Gt; Types or parameters are duplicated, it can at least duplicate your normal structures, then you can give those member functions to return or generate the vectors. Second, use std :: tuple if there are many parameter lists variants and you have an example, while making a common routine (like filling vector), getting them all in one way Tuples that want to store derivative examples may be useful:

  typedef std:: tuple & lt; Derivative & LT; Bool & gt; Derivative & LT; Bool & gt; Derivative & LT; Bool & gt; Derivative & LT; Bool & gt; Derivative & LT; Bool & gt; Derivative & LT; Integer & gt; derived & lt; Std :: string & gt;, derivative & lt; Bool & gt; & Gt; Atuple; Atplay TV {"online", "official", "no-rotation", "no-background", "no_sound", "no_music", "black and white"}; Const size_ size (std :: tuple_size & lt; atuple & gt; :: value); / * Or you can use the std :: array because the size is constant * / std :: vector & lt; Base * & gt; ConfigValues; ConfigValues.reserve (size); Push (t, configValues);   

and Push () will look like this:

  template   

(borrowed).

If you do not face this problem in many parts of your program, then these solutions will not be so useful to you.

Third - Use Arrays:

  std :: array & lt; Derived & lt; Bool & gt; 5> A {{{"online"}, {"official"}, {"no_rotation"}, {"no_background"}, {"black_and_white"}}}; Derivative & LT; Integer & gt; Nosound {"no_sound"}; Derivative & LT; String & gt; NoMusic {"no_music"}; Of vector & lt; Base * & gt; ConfigValues ​​{& amp; The nosound, & amp; NoMusic}; For (Derived & lt; bool & gt; B: A) configValues.push_back (& ​​amp; amp; b); // Original order was not placed    

No comments:

Post a Comment