Tuesday 15 June 2010

c++ - container pattern with specialized children -


I have several specific classes, for example

  class transition_matrix: public transition {setmatrix (Matrix * pStartMatrix); }; Class Transition_Vitek: Public Transition {Set Winter (Vector * Pasteector); } Class Transition_Container: public transition {}   

I would like to call Setetector () or SetMatrix () without declaring the function for each type of object to call Animate_Container. For example, I do not want to declare Animate_Container as follows ...

  Class Transition_Container: Public Transition {SetMatrix (matrix * pStartMatrix); // loops through all children SetMatrix SetVector (Vector * PSTartVector) // loops through all children SetVector}   

I do not want Animate_Container to know which child is but I want the facility to call these functions on the container so I should search through kids and call the matrix or a vector "transition".

What do I need to use the right patent here?

Actually I want to set a matrix or vector on the original container and use it to move towards each child.

Thoughts?

There will be a basic enough here. To implement this, you declare member functions as virtual in Base Class transition . This will allow you to maintain a list of Transition_Container in the transition objects, repeat through the list and call the appropriate member function.

  # & lt; Vector & gt; Class Transition {Public: // Default Implementation which does not have Virtual Void Setmatrix (Matrix *) {}}; Category Transition_Container: Public Transit {std :: vector & lt; Transit * & gt; Transitions_; Public: Virtual Zero SetMatrix (matrix * pStartMatrix) {for (std :: vector & lt; transition *>: iterator it = transitions_.begin (); it! = Transitions_.end (); ++ this) { (* This) - & gt; SetMatrix (pStartMatrix); }}};   

If you do not want to transition to learn about different data types that can be used, then you can promote For: Any and boost: any_cast . This is similar to the above suggestion, but removes the dependence of matrix and vector to transition and takes responsibility for dealing with various types. The implementation of the classes which receive from it. I only recommend doing this if there is a requirement that exactly transition to the matrix and vector types Prevents you from knowing about

  #include & lt; Vector & gt; #include & lt; Boost / any.hpp & gt; Class Transition {Public: // Default Implementation Which Virtual Void Settlement (Bust :: Someone) {}} does nothing; Class transition_metrics: public transition {virtual zero set valley (boost :: any other value) {try {matrix * matrix = boost :: any_cast & lt; Matrix * & gt; (Values); // do stuff} hold (const boost :: bad_any_cast &) {return; }}}; Category Transition_Container: Public Transit {std :: vector & lt; Transit * & gt; Transitions_; Public: Template & lt; Arg & gt; Zero SetValueT (Arg * arg) {boost :: any value = arg; SetValue (value); } Virtual Zero Setwela (boost :: any other value) {std :: vector : iterator = transitions_.begin (); it! = Transitions_.end (); ++ This) {(* this) - & gt; SetValue (value); }}};   

To maintain the list of infections, I use shared_ptr or unique_ptr from Boost or C ++ 11 standard library I recommend as soon as the objects

  std :: vector & lt; Std :: shared_ptr & lt; Infection & gt; & Gt; Transitions_;   

I did not include it in the above example because I do not know if you are still familiar with using it. If you are not doing this, then you are seeing it.

No comments:

Post a Comment