Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to obtain the type of a local variable in a method in C++?

Reply
Thread Tools

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

 
 
dolphin
Guest
Posts: n/a
 
      08-18-2004
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
 
Reply With Quote
 
 
 
 
Dennis Jones
Guest
Posts: n/a
 
      08-18-2004
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: 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



 
Reply With Quote
 
 
 
 
Ryan
Guest
Posts: n/a
 
      08-18-2004
(dolphin) wrote in message news:<. 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
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      08-19-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Instance Variable vs Local Variable Paul Carey Java 3 12-03-2003 05:05 PM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM
Re: How do I access another type's method from one type's method Howard C++ 2 07-04-2003 12:08 PM
Re: How do I access another type's method from one type's method Rolf Magnus C++ 1 07-04-2003 02:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57