473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Dear all,

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

Thanks,

Shuisheng

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
3
by: me | last post by:
Hi there. I'm writing some code to interpret binary data. The spec of the data is in the form of field names and field sizes. The design is to take a byte array of data, and a struct...
0
by: K. Deepa | last post by:
Hi All, Is it possible to find the size of a pgsql table. TIA -- regards, Deepa K
9
by: Phil | last post by:
I have a custom class that has been assigned to a variable and worked with, I now need to cast this class as a different class, which is in a separate assembly, not currently in the project. ...
2
by: Ing. Rajesh Kumar | last post by:
Hi everybody Is it possible to set the size of text boxes when in edit mode ? Thanks in advance Raja
4
by: Captain Dondo | last post by:
Is it possible to get the actual document size? Not the window size, but the actual rendered document size, which in my case is bigger than the window. I am trying to set up a form entry system...
28
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
1
by: Ahmad Nawaz | last post by:
I have a dropdown list which have some elements. I want to get all the elements of the dropdown list into a variable of type array. Any Idea???
1
by: Patrick Cornbil | last post by:
Is it possible to adjust the size of an image or just a circle based on the value or a formaula in a text box? One = small circle Ten = big circle I am trying to adjust the size of a danger...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.