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