By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <do************@yahoo.com> wrote in message
news:81**************************@posting.google.c om...
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

Jul 22 '05 #2

P: n/a
do************@yahoo.com (dolphin) wrote in message news:<81**************************@posting.google. com>...
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
Jul 22 '05 #3

P: n/a
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

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.