Is it possible to get a variable's type and size in code?

S

shuisheng

Dear all,

I am wondering is it possible to get a variable's type and size in
code.

Thanks,

Shuisheng
 
A

aaragon

Victor said:
Yes. What are you trying to accomplish?

V

For the size you can get sizeof(variable). For the type, I understand
that the GNU g++ compiler now supports typeof(variable). The last one
I never tried before so I don't know if it works. Hope it helps...

 
S

shuisheng

Dear Victor Bazarov,

Thank you for your reply.

For example, I have a base class Shape and its derived class Sphere and
Cube. I use the Shape pointer substantialize a Sphere. How can I know
it is Sphere rather than Cube?

Thanks,

Shuisheng
 
K

Kai-Uwe Bux

shuisheng wrote: [top posting corrected]
For example, I have a base class Shape and its derived class Sphere and
Cube. I use the Shape pointer substantialize a Sphere. How can I know
it is Sphere rather than Cube?

a) Please don't top post. Had you read Victor's signature, you would have
known that top posting will only get your post ignored. His sentiments are
shared by most regulars in this group. Top posting is considered poor form
around these parts. It's a cultural thing.

b) You want to read up on dynamic_cast.

c) If you find yourself wanting to test whether a Shape* actually points to
a cube, you are usually in a state of sin, i.e., your design is somewhat
broken. Ideally, you should never need to do that. The interface of Shape
should be flexible enough so that all specializations can go about all
their businesses using the Shape interface.


Best

Kai-Uwe Bux
 
T

Thomas Matthews

Kai-Uwe Bux said:
shuisheng wrote: [top posting corrected]

For example, I have a base class Shape and its derived class Sphere and
Cube. I use the Shape pointer substantialize a Sphere. How can I know
it is Sphere rather than Cube?


a) Please don't top post. Had you read Victor's signature, you would have
known that top posting will only get your post ignored. His sentiments are
shared by most regulars in this group. Top posting is considered poor form
around these parts. It's a cultural thing.

b) You want to read up on dynamic_cast.

c) If you find yourself wanting to test whether a Shape* actually points to
a cube, you are usually in a state of sin, i.e., your design is somewhat
broken. Ideally, you should never need to do that. The interface of Shape
should be flexible enough so that all specializations can go about all
their businesses using the Shape interface.


Best

Kai-Uwe Bux
For the O.P.:
One of the challenges of Object Oriented design and Programming
is to construct programs that are free of run-time type checking.
This exercise will bring about a new perspective on writing
programs.

--
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
http://www.sgi.com/tech/stl -- Standard Template Library
 

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

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top