Hi, I have a condition and the requirement should be, a condition variable to a different type to cast depending. Since I do not want to write anew every time Code: if (condition == 1) Object1 var = (Object1) getVar (); else if (condition == 2) Object2 var = (Object2) getVar (), etc. .. I wanted to ask if there any other possibility, one that saves me and I do not think straight. (The best one member variable to the appropriate type always corresponds.)
How about this: Code: Var object1 = (Object1) getVar (); Object2 var = (Object2) getVar (); Anaon you had yourself a little bit of concrete to express what you really want.
I guess it's quite simply dadrum of an object whose true type is known casting automated to. as is a Enum Type (Bool, Integer, Float) should now be something like going Code: if (type == bool) bool var = static_cast < bool > (getVar ()) if (Type == integer) integer var = static_cast < bool > (getVar ()); ... I just think that the type derivations one different and the same base class .
correct! also right! But for special functions of derived classes to come to the (or the derived class to create an object), I have to base object to cast it. So the question is only how to prevent I do every time I do have a query 20 different types start between the need to find out what type it is just an object and what type I need to create.
You have to adjust the design was. anything you write about what you actually try to make as we can only guess. Visitor pattern, perhaps. http://en.wikipedia.org/wiki/Visitor_pattern
Without C + + and the Rumgecaste of classes would pure C on Rumgecaste where actually executed code ie the functions themselves run out in, and should your requirements but meet it? as Code: enum (PLUS, MINUS, TIME) void plus (int i, int j) printf ("% d \ n (", i + j);) void minus (int i, int j) (printf ("% d \ n, ij);) void times (int i, int j) printf ("% d \ n (", i * j);) # define dispatch (x) ((x) == PLUS? plus: (x ) == MINUS? minus: times) dispatch (PLUS) (2.5), dispatch (MINUS) (1.99), dispatch (MAL) (5.5); the parameter to dispatch you can so your terms dynamic design accordingly. With C + + is even without casts, if you dispatching method level ranges, you have everything in one class: Code: enum (PLUS, MINUS, TIME) class D ( int x; void plus (int a, int b) (printf ("% d", a + b);) void minus (int a, int b) (printf ( "% d", from);) void times (int a, int b) (printf ("% d", a * b);) public: D (int p) (x = p;) void doit (int a , int b) ( switch (x) ( case PLUS: plus (a, b) break; case MINUS: minus (a, b) break; case MAL: mal (a, b) break; ))); Code: D plus (PLUS), minus (MINUS), sometimes (MAL); plus.doit (1.2); minus.doit (3.4); mal.doit (5.6); Again, your conditions you can give to the constructor.