⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7types.html

📁 Visual C++ has been one of most effective tool for the large industrial applications. This book is t
💻 HTML
字号:
<html>
<head>
    <title>Types</title>
    <meta  name="description" content="Built-in types in C++">
    <meta name="keywords" content="type, typedef, variable, long, short, double, unsigned, char, byte">
    <link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>

<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#FFFFDC">

<!-- Main Table -->
<table cellpadding="6">
    <tr>
    <td width="78">&nbsp;</td>
    <td>
<h3>Types</h3>

<p class=topics>
Built in types, typedefs.
<p>
Here is the list of built-in types with a short description of each of them.
<ul>
<li><var>bool</var>-Boolean type. Can take predefined values <var>true</var> and <var>false</var>.
<li><var>int</var> -generic signed integral type. Its size is machine dependent, it is considered the most efficient implementation of an integral type for a given architecture. At least 2 bytes long.
<li><var>double</var> - generic signed floating point type.
<li><var>char</var> -generic 8-bit character type. Used in ASCII strings. Depending on the compiler, the default <i>signedness</i> of <var>char</var> may be signed or unsigned.
<li><var>short</var> and <var>long</var> are a little better defined signed integral types. Usually <var>short</var> is 2-byte long and <var>long</var> is 4-byte long, but on a 64-bit machine it may be 8-bytes long (same as <var>int</var>). If you are writing your program to be run on only one type of processor architectures, you may make assumptions about the sizes of <var>shorts</var> and <var>long</var>s (and <var>char</var>s, of course). Sizes become crucial when you start storing data on removable disks or move them across the network. 
<br>
<var>Short</var> is often used as a cheap <var>int</var>, space-wise; for example, in large arrays. <var>Long</var> is often used when <var>short</var> or <var>int</var> may overflow. Remember, in two bytes you can store numbers between -32767 and 32767. Your arithmetic can easily overflow such storage, and the values will wrap around.
<li><var>float</var> is a cheaper version of <var>double</var> (again, space-wise), but it offers less precision. It makes sense to use it in large arrays, where size really matters.
<li>Additional floating point type of even larger size and precision is called <var>long double</var>.
<li>All integral types may be prefixed by the keyword <var>unsigned</var> or <var>signed</var> (default for all types--except, as mentioned above, char--is  <var>signed</var>). Of unsigned types, of special interest is <var>unsigned char</var> corresponding to a machine byte, and <var>unsigned long</var> often used for low-level bit-wise operations.
</ul>
<p>
When you are tired of typing <var>unsigned long</var> over and over again, C++ lets you define a shortcut called a <var>typedef</var>. It is a way of giving a different name to any type--built-in or user defined. For instance, an <var>unsigned long</var> is often <var>typedef</var>'d to (given the name of) <var>ULONG</var>:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>typedef unsigned long ULONG;</pre>
</table>
<!-- End Code -->

<p>
After you define such a <var>typedef</var>, you can use <var>ULONG</var> anywhere you would use any other type, for example,
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre><b>ULONG</b> ValueHolder::SwapValue (<b>ULONG</b> newValue)
{
    <b>ULONG</b> oldValue = _ulNumber;
    _ulNumber = newValue;
    return oldValue;
}</pre>
</table>
<!-- End Code -->

<p>
Similarly one often uses <var>typedef</var>s for <var>BYTE</var>
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>typedef unsigned char BYTE;</pre>
</table>
<!-- End Code -->

<p>
Like every feature of C++, typedefs can be abused. Just because you can redefine all types using your own names, doesn't mean you should. What do you make of types like <var>INT</var> or <var>LONG</var> when you see them in somebody else's code? Are these typedef'd names for <var>int</var> or <var>long</var>? If so, why not use directly <var>int</var> or <var>long</var>? And if not, then what the heck is going on? I can't think of any rational reason to typedef such basic data types, other than following some kind of coding standard from hell.
<p>Besides these basic types there is an infinite variety of derived types and user defined types. We've had a glimpse of derived types in an array of chars. And we've seen user-defined types called classes.

</table>
<!-- End Main Table -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -