How to obtain the type of a local variable in a method in C++?

Discussion in 'C++' started by dolphin, Aug 18, 2004.

  1. dolphin

    dolphin Guest

    Hi,
    I have the name of a local variable in a method. How can I retrieve
    the type of it?
    For example, in the following code fragment:

    *************************************************
    CString iftype, ipaddr;
    int ifspeed;
    port->GetAttribute("IFType", iftype);
    port->GetAttribute("IFSpeed", ifspeed);
    port->GetAttribute("IPAddress", ipaddr);
    *************************************************

    I'm expecting such a function like GetType("iftype") and it will
    return "CString". Does such reflection kind of API exist?
    Thanks a lot!

    --dolphin
    dolphin, Aug 18, 2004
    #1
    1. Advertising

  2. dolphin

    Dennis Jones Guest

    If your compiler supports RTTI, there is a 'typeid' operator that should
    give you what you want.

    There is also an article in the September '04 issue of the C/C++ Users
    Journal entitled, "Reflecting Attributes and Base Classes" which discusses a
    library to support reflection. I haven't read the article myself so I don't
    know if it will give you what you want, but it might also be worth checking
    out.

    - Dennis

    "dolphin" <> wrote in message
    news:...
    > Hi,
    > I have the name of a local variable in a method. How can I retrieve
    > the type of it?
    > For example, in the following code fragment:
    >
    > *************************************************
    > CString iftype, ipaddr;
    > int ifspeed;
    > port->GetAttribute("IFType", iftype);
    > port->GetAttribute("IFSpeed", ifspeed);
    > port->GetAttribute("IPAddress", ipaddr);
    > *************************************************
    >
    > I'm expecting such a function like GetType("iftype") and it will
    > return "CString". Does such reflection kind of API exist?
    > Thanks a lot!
    >
    > --dolphin
    Dennis Jones, Aug 18, 2004
    #2
    1. Advertising

  3. dolphin

    Ryan Guest

    (dolphin) wrote in message news:<>...
    > Hi,
    > I have the name of a local variable in a method. How can I retrieve
    > the type of it?
    > For example, in the following code fragment:
    >
    > *************************************************
    > CString iftype, ipaddr;
    > int ifspeed;
    > port->GetAttribute("IFType", iftype);
    > port->GetAttribute("IFSpeed", ifspeed);
    > port->GetAttribute("IPAddress", ipaddr);
    > *************************************************
    >
    > I'm expecting such a function like GetType("iftype") and it will
    > return "CString". Does such reflection kind of API exist?
    > Thanks a lot!
    >
    > --dolphin


    Hi Dolphin,

    You could try "typeid" in <typeinfo>:

    void func()
    {
    int i;

    std::cout << std::typeid(i).name();
    }

    I beleive the name provided is implementation specific.

    Ryan
    Ryan, Aug 18, 2004
    #3
  4. dolphin wrote:

    > Hi,
    > I have the name of a local variable in a method. How can I retrieve
    > the type of it?
    > For example, in the following code fragment:
    >
    > *************************************************
    > CString iftype, ipaddr;
    > int ifspeed;
    > port->GetAttribute("IFType", iftype);
    > port->GetAttribute("IFSpeed", ifspeed);
    > port->GetAttribute("IPAddress", ipaddr);
    > *************************************************
    >
    > I'm expecting such a function like GetType("iftype") and it will
    > return "CString". Does such reflection kind of API exist?
    > Thanks a lot!
    >
    > --dolphin


    In most situations, requiring knowledge of a type
    is an indication of a poor design.

    Have you tried coding using pointers to base classes
    and virtual functions so that knowledge of an object's
    type is not required?

    There is also overloading global functions:

    class InterfaceType;
    class IntefaceSpeed;
    class IPAddress;

    void GetAttribute(InterfaceType& it, Port * p)
    {
    //...
    }

    void GetAttribute(InterfaceSpeed& i_s, Port *p)
    {
    }

    void GetAttribute(IPAddress& ipa, Port * p)
    {
    }

    // Code fragment:
    InterfaceSpeed ifspeed;
    InterfaceType iftype;
    IPAddress ipaddr;
    std::string iftype_text;
    std::string ipaddr_text;

    GetAttribute(ifspeed, port);
    GetAttribute(iftype, port);
    GetAttribute(ipaddr, port);

    iftype_text = iftype.convert_to_string();
    ipaddr_text = ipaddr.convert_to_string();

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 19, 2004
    #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. Replies:
    2
    Views:
    503
    moongate
    Jan 18, 2005
  2. Howard
    Replies:
    2
    Views:
    366
    Michiel Salters
    Jul 4, 2003
  3. Rolf Magnus
    Replies:
    1
    Views:
    318
    ding feng
    Jul 4, 2003
  4. Patrick Hoffmann
    Replies:
    3
    Views:
    2,815
    Christian Jan├čen
    Aug 8, 2003
  5. Martin
    Replies:
    3
    Views:
    365
    Pete Becker
    Aug 3, 2007
Loading...

Share This Page