Connecting Tech Pros Worldwide Help | Site Map

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

dolphin
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Guest
 
Posts: n/a
#2: Jul 22 '05

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


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" <dolphinzhang79@yahoo.com> wrote in message
news:8162dec3.0408181050.19e08ca0@posting.google.c om...[color=blue]
> 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[/color]


Ryan
Guest
 
Posts: n/a
#3: Jul 22 '05

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


dolphinzhang79@yahoo.com (dolphin) wrote in message news:<8162dec3.0408181050.19e08ca0@posting.google. com>...[color=blue]
> 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[/color]

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
Thomas Matthews
Guest
 
Posts: n/a
#4: Jul 22 '05

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


dolphin wrote:
[color=blue]
> 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[/color]

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

Closed Thread