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

D

dolphin

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
 
D

Dennis Jones

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
 
R

Ryan

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
 
T

Thomas Matthews

dolphin said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top