string to class type using typeinfo

Discussion in 'C++' started by CT, Jan 18, 2006.

  1. CT

    CT Guest

    Hi

    I have a function to test if a pointer* is of type string*. I would like
    to use dynamic casting to figure it out. How can i convert a string to a
    type to useit in the dynamic_cast<"string"> function. His this possible
    in c,c++ ?

    this function is used when i deserilaize an object, i only keep the
    class name. validation purposes.

    the first argument is an interface on which all passed object must
    implement.

    bool VerifyPointer( baseClass *pData, const char * pszClass )
    {
    return dynamic_cast<pszClass*>( (baseClass *) pData) ? true : false;

    // replace pszclass by macro or whatever to convert a string to
    type

    }

    thanks,
    CT, Jan 18, 2006
    #1
    1. Advertising

  2. CT wrote:
    > I have a function to test if a pointer* is of type string*. I would like
    > to use dynamic casting to figure it out. How can i convert a string to a
    > type to useit in the dynamic_cast<"string"> function. His this possible
    > in c,c++ ?


    I don't think there is such thing as 'string' type in C, and I know for
    sure there is no such thing as 'dynamic_cast' in C. So, let's drop 'C'
    for now.

    Now, what do you mean by "pointer*"? Do you mean "void*"? If so, no, it
    is not possible to find out what pointer _was_ there before you converted
    it to a pointer to void. To convert back you use 'static_cast' and
    provide the actual type yourself.

    > this function is used when i deserilaize an object, i only keep the
    > class name. validation purposes.
    >
    > the first argument is an interface on which all passed object must
    > implement.
    >
    > bool VerifyPointer( baseClass *pData, const char * pszClass )
    > {
    > return dynamic_cast<pszClass*>( (baseClass *) pData) ? true : false;


    (a) 'pData' is already of type 'baseClass*', no need to cast it again.

    (b) " true : false " looks really strange. Why not just drop the ternary
    operator altogether?

    (c) There is no conversion of a string to type. Not in C++, anyway. What
    you need is probably a big if/else statement to perform different
    casts based on the contents of 'pszClass' string. Something like

    if (strcmp(pszClass, "string") == 0)
    return dynamic_cast<derivedClass_string*>(pData);
    else if (strcmp(pszClass, "somethingelse") == 0)
    return dynamic_cast<derivedClass_other*>(pData);
    ...

    >
    > // replace pszclass by macro or whatever to convert a string to
    > type
    >
    > }


    V
    Victor Bazarov, Jan 18, 2006
    #2
    1. Advertising

  3. CT

    Guest

    CT wrote:
    > Hi
    >
    > I have a function to test if a pointer* is of type string*. I would like
    > to use dynamic casting to figure it out. How can i convert a string to a
    > type to useit in the dynamic_cast<"string"> function. His this possible
    > in c,c++ ?


    Since templates are compile time things this wouldn't serve much
    purpose; least it seems so to me (someone might find a reason).
    However, there is the boost lexical_cast, which can attempt something
    like lexical_cast<type>("string")...in other words attempts to parse a
    runtime string and turn it into the compile time hardcoded 'type'.
    , Jan 18, 2006
    #3
  4. CT

    Guest

    i think it's unpossible
    , Jan 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,711
    Thomas Weidenfeller
    Oct 12, 2005
  2. Asfand Yar Qazi

    RTTI typeinfo pointers

    Asfand Yar Qazi, Jun 1, 2004, in forum: C++
    Replies:
    2
    Views:
    548
    Asfand Yar Qazi
    Jun 1, 2004
  3. jd
    Replies:
    3
    Views:
    340
  4. Steven T. Hatton

    <typeinfo>

    Steven T. Hatton, May 10, 2005, in forum: C++
    Replies:
    2
    Views:
    2,917
    Steven T. Hatton
    May 11, 2005
  5. Ganesh
    Replies:
    3
    Views:
    490
    Rolf Magnus
    May 10, 2005
Loading...

Share This Page