Depending on the condition of different Cast

Discussion in 'MFC' started by creative, May 28, 2010.

  1. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    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.)
     
  2. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  3. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    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 .
     
  4. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  5. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
  6. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice