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

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

P: n/a
Dear all,

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

Thanks,

Shuisheng

Sep 23 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
shuisheng wrote:
I am wondering is it possible to get a variable's type and size in
code.
Yes. What are you trying to accomplish?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 23 '06 #2

P: n/a

Victor Bazarov wrote:
shuisheng wrote:
I am wondering is it possible to get a variable's type and size in
code.

Yes. What are you trying to accomplish?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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...



Sep 23 '06 #3

P: n/a
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
Victor Bazarov wrote:
shuisheng wrote:
I am wondering is it possible to get a variable's type and size in
code.

Yes. What are you trying to accomplish?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 23 '06 #4

P: n/a
shuisheng wrote: [top posting corrected]
>Victor Bazarov wrote:
shuisheng wrote:
I am wondering is it possible to get a variable's type and size in
code.

Yes. What are you trying to accomplish?

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
Sep 23 '06 #5

P: n/a
Kai-Uwe Bux wrote:
shuisheng wrote: [top posting corrected]

>>>Victor Bazarov wrote:
shuisheng wrote:

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

Yes. What are you trying to accomplish?

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
Sep 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.