📄 tut3-6.html
字号:
<HTML>
<HEAD>
<TITLE>C++ Tutorial: 3.6, User defined data types.</TITLE>
<META NAME="description" CONTENT="typedef, union, enum">
<META NAME="keywords" CONTENT="typedefs, unions, enums, own types">
</HEAD>
<BODY BGCOLOR="white">
<!--captut-->
<CENTER>
<TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=1 BORDER=0>
<TR><TD WIDTH=90%>
<FONT SIZE=4> Section 3.6 </FONT><BR>
<FONT SIZE=5><B> User defined data types</B></FONT>
</TD><TD><!--ad--><!--#include virtual="/ad/ad468.shtml"--><!--/ad-->
</TD><TD VALIGN="bottom"><A HREF="http://www.cplusplus.com/doc/tutorial/">
<IMG SRC="head.gif" ALT="cplusplus.com" BORDER=0></A></TD></TR>
<TR><TD BGCOLOR="#0000FF" ALIGN="center" COLSPAN=3>
<IMG SRC="head0.gif" WIDTH=2 HEIGHT=2 BORDER=0></TD></TR>
</TABLE>
</CENTER>
<!--/captut-->
<P>
We have already seen a data type that is defined by the user (programmer):
the structures. But in addition to these there are other kinds of user defined
data types:
<p>
<h2>Definition of own types (<tt>typedef</tt>).</h2>
C++ allows us to define our own types based on
other existing data types. In order to do that we shall use keyword <TT><B>typedef</B></TT>,
whose form is:
<blockquote><tt>
<b>typedef </b><i>existing_type new_type_name</i> ;
</tt></blockquote>
where <tt><i>existing_type</i></tt> is a C++ fundamental or any other defined type
and <tt><i>new_type_name</i></tt> is the name that the new type
we are going to define will receive. For example:
<blockquote><tt>
typedef char C;<br>
typedef unsigned int WORD;<br>
typedef char * string_t;<br>
typedef char field [50];
</tt></blockquote>
In this case we have defined four new data types: <tt><b>C</b></tt>,
<tt><b>WORD</b></tt>, <tt><b>string_t</b></tt> and <TT><B>field</B></TT>
as <tt><b>char</b></tt>, <tt><b>unsigned int</b></tt>, <tt><b>char*</b></tt> and
<tt><b>char[50]</b></tt> respectively, that we could perfectly use later as valid types:
<blockquote><tt>
C achar, anotherchar, *ptchar1;<br>
WORD myword;<br>
string_t ptchar2;<br>
field name;
</tt></blockquote>
<TT>typedef</TT> can be useful to define a type that is repeatedly used within a program
and it is possible that we will need to change it in a later version, or if a type
you want to use has too long a name and you want it to be shorter.
<p>
<h2>Unions</h2>
Unions allow a portion of memory to be accessed as different data types,
since all of them are in fact the <U>same</U> location in memory. Its declaration and use is similar
to the one of structures but its functionality is totally different:
<BLOCKQUOTE><TT><PRE>
<B>union </B><I>model_name </I><B>{</B>
<I>type1 element1</I><B>;</B>
<I>type2 element2</I><B>;</B>
<I>type3 element3</I><B>;
.
.
}</B> <I>object_name</I><B>;</B>
</PRE></TT></BLOCKQUOTE>
All the elements of the <i>union</i> declaration occupy the same space of memory.
Its size is the one of the greatest element of the declaration. For example:
<BLOCKQUOTE><TT><PRE>
union mytypes_t {
char c;
int i;
float f;
} mytypes;
</PRE></TT></BLOCKQUOTE>
defines three elements:
<blockquote><tt><font color="blue">
mytypes.c<br>
mytypes.i<br>
mytypes.f<br>
</font></tt></blockquote>
each one of a different data type. Since all of them are referring to a same location in
memory, the modification of one of the elements will afect the value of all of them.
<p>
One of the uses a <I>union</I> may have is to unite an elementary type with
an array or structures of smaller elements. For example,
<BLOCKQUOTE><TT><PRE>
union mix_t{
long l;
struct {
short hi;
short lo;
} s;
char c[4];
} mix;
</PRE></TT></BLOCKQUOTE>
defines three names that allow us to access the same group of 4 <i>bytes</i>:
<tt><b>mix.l</b></tt>, <tt><b>mix.s</b></tt> and <tt><b>mix.c</b></tt> and which we can
use according to how we want to access it, as <tt><b>long</b></tt>, <tt><b>short</b></tt>
or <tt><b>char</b></tt> respectively. I have mixed types, arrays and structures in the
union so that you can see the different ways that we can access the data:
<blockquote><img src="imgunio1.gif"></blockquote>
<p>
<h2>Anonymous unions</h2>
<IMG SRC="icoc-cpp.gif" ALIGN="left">
In C++ we have the option that unions be anonymous. If we include a union in a
structure without any object name (the one that goes after the curly brackets <tt>{ }</tt>)
the union will be anonymous and we will be able to access the elements directly by its
name. For example, look at the difference between these two declarations:
<P><CENTER>
<TABLE WIDTH=100% CELLSPACING=5 CELLPADDING=5>
<TR><TD ALIGN="center" WIDTH=50%><B><U>union</U></B></TD>
<TD ALIGN="center" WIDTH=50%><B><U>anonymous union</U></B></TD></TR>
<TR><TD WIDTH=50% BGCOLOR="#FFFFBF"><TT><PRE>
struct {
char title[50];
char author[50];
union {
float dollars;
int yens;
} price;
} book;
</PRE></TT></TD>
<TD WIDTH=50% BGCOLOR="#FFFFBF"><TT><PRE>
struct {
char title[50];
char author[50];
union {
float dollars;
int yens;
};
} book;
</PRE></TT></TD></TR></TABLE>
</CENTER>
<p>
The only difference between the two pieces of code is that in the first one we gave a name
to the union (<tt><b>price</b></tt>) and in the second we did not. The difference is when
accessing members <tt><b>dollars</b></tt> and <tt><b>yens</b></tt> of an object.
In the first case it would be:
<BLOCKQUOTE><TT>book.price.dollars<BR>book.price.yens</TT></BLOCKQUOTE>
whereas in the second it would be:
<BLOCKQUOTE><TT>book.dollars<BR>book.yens</TT></BLOCKQUOTE>
Once again I remind you that because it is a union, the fields <tt><b>dollars</b></tt>
and <tt><b>yens</b></tt> occupy the same space in the memory so they cannot be used
to store two different values. That means that you can include a price in dollars or yens,
but not both.
<p>
<h2>Enumerations (<tt>enum</tt>)</h2>
Enumerations serve to create data types to contain something different that is not limited
to either numerical or character constants nor to the constants <tt><b>true</b></tt>
and <tt><b>false</b></tt>. Its form is the following:
<BLOCKQUOTE><TT><PRE>
<B>enum</B> <I>model_name</I> <B>{</B>
<I>value1</I><B>,</B>
<I>value2</I><B>,</B>
<I>value3</I><B>,
.
.
}</B><I> object_name</I><B>;</B>
</PRE></TT></BLOCKQUOTE>
For example, we could create a new type of variable called <tt><b>color</b></tt>
to store colors with the following declaration:
<blockquote><tt>
enum colors_t {black, blue, green, cyan, red, purple, yellow, white};
</tt></blockquote>
Notice that we do not include any fundamental data type in the declaration.
To say it another way, we have created a new data type without it being based on any
existing one:
the type <tt><b>color_t</b></tt>, whose possible values are the colors that we have
enclosed within curly brackets <tt>{}</tt>. For example, once declared the
<tt><b>colors_t</b></tt> enumeration in the following expressions will be valid:
<BLOCKQUOTE><TT>
colors_t mycolor;<BR>
<BR>
mycolor = blue;<BR>
if (mycolor == green) mycolor = red;
</TT></BLOCKQUOTE>
In fact our enumerated data type is compiled as an integer and its possible values
are any type of integer constant specified. If it is not specified, the
integer value equivalent to the first possible value is <tt><b>0</b></tt>
and the following ones follow a +1 progression. Thus, in our data type
<tt><b>colors_t</b></tt> that we defined before, <tt><b>black</b></tt> would be
equivalent to <tt><b>0</b></tt>, <tt><b>blue</b></tt> would be equivalent to
<tt><b>1</b></tt>, <tt><b>green</b></tt> to <tt><b>2</b></tt> and so on.
<p>
If we explicitly specify an integer value for some of the possible values of our
enumerated type (for example the first one) the following values will be the increases
of this, for example:
<BLOCKQUOTE><TT><PRE>
enum months_t { january=1, february, march, april,
may, june, july, august,
september, october, november, december} y2k;
</PRE></TT></BLOCKQUOTE>
in this case, variable <tt><b>y2k</b></tt> of the enumerated type
<tt><b>months_t</b></tt> can contain any of the 12 possible values that go from
<tt><b>january</b></tt> to <tt><b>december</b></tt> and that are equivalent to values
between <tt><b>1</b></tt> and <tt><b>12</b></tt>, not between <tt><b>0</b></tt>
and <tt><b>11</b></tt> since we have made <tt><b>january</b></tt> equal
to <tt><b>1</b></tt>.
<!--cuatut-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD BGCOLOR="#0000FF"><IMG SRC="head0.gif" WIDTH=2 HEIGHT=2></TD></TR>
<TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>© The C++ Resources Network, 2000-2003 - All rights reserved</FONT></TD></TR>
</TABLE></CENTER>
<P>
<CENTER>
<TABLE CELLPADDING=0 WIDTH=100%>
<TR><TD ALIGN="right" WIDTH=45%><A HREF="tut3-5.html">
<IMG SRC="butnback.gif" ALIGN="right" BORDER=0>
Previous:<BR><B>3-5. Structures</B></A></TD>
<TD ALIGN="center" WIDTH=10%><A HREF="index.html">
<IMG SRC="butnindx.gif" BORDER=0><BR>
index</A></TD>
<TD ALIGN="left" WIDTH=45%><A HREF="tut4-1.html">
<IMG SRC="butnnext.gif" ALIGN="left" BORDER=0>
Next:<BR><B>4-1. Classes.</B></A>
</TD></TR></TABLE>
</CENTER>
<!--/cuatut-->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -