版本6和7间的区别
于2007-07-18 21:24:28修订的的版本6
大小: 3172
编辑: czk
备注:
于2007-07-23 15:46:43修订的的版本7
大小: 3838
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 4: 行号 4:
== 6.1 Basics of Structures 结构 == == 6.1 Basics of Structures 结构本知识 ==
行号 7: 行号 7:

attachment:pic61.gif

我们首先来建立一些适用于图形领域的结构。点是最基本的对象,假定用x与y坐标表示它,且x、y的坐标值都为整数(参见图6-1)。
行号 17: 行号 21:

我们可以采用结构存放这两个坐标,其声明如下:{{{#!cplusplus
struct point {
    int x;
    int y;
};
}}}
行号 18: 行号 30:

关键字struct引入结构声明。结构声明由包含在花括号内的一系列声明组成。关键字struct后面的名字是可选的,称为结构标记(这里是point)。结构标记用于为结构命名,在定义之后,结构标记就代表花括号内的声明,可以用它作为该声明的简写形式。

Navigation(slides)

6.1 Basics of Structures 结构的基本知识

Let us create a few structures suitable for graphics. The basic object is a point, which we will assume has an x coordinate and a y coordinate, both integers.

attachment:pic61.gif

我们首先来建立一些适用于图形领域的结构。点是最基本的对象,假定用x与y坐标表示它,且x、y的坐标值都为整数(参见图6-1)。

attachment:pic61.gif

The two components can be placed in a structure declared like this:

   1 struct point {
   2     int x;
   3     int y;
   4 };

我们可以采用结构存放这两个坐标,其声明如下:

   1 struct point {
   2     int x;
   3     int y;
   4 };

The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.

关键字struct引入结构声明。结构声明由包含在花括号内的一系列声明组成。关键字struct后面的名字是可选的,称为结构标记(这里是point)。结构标记用于为结构命名,在定义之后,结构标记就代表花括号内的声明,可以用它作为该声明的简写形式。

The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type. That is,

struct { ... } x, y, z;

is syntactically analogous to

int x, y, z;

in the sense that each statement declares x, y and z to be variables of the named type and causes space to be set aside for them.

A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, given the declaration of point above,

struct point pt;

defines a variable pt which is a structure of type struct point. A structure can be initialized by following its definition with a list of initializers, each a constant expression, for the members:

struct point maxpt = { 320, 200 };

An automatic structure may also be initialized by assignment or by calling a function that returns a structure of the right type.

A member of a particular structure is referred to in an expression by a construction of the form

structure-name.member

The structure member operator "." connects the structure name and the member name. To print the coordinates of the point pt, for instance,

printf("%d,%d", pt.x, pt.y);

or to compute the distance from the origin (0,0) to pt,

double dist, sqrt(double);

dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y);

Structures can be nested. One representation of a rectangle is a pair of points that denote the diagonally opposite corners:

attachment:pic62.gif

   1 struct rect {
   2     struct point pt1;
   3     struct point pt2;
   4 };

The rect structure contains two point structures. If we declare screen as

struct rect screen;

then

screen.pt1.x

refers to the x coordinate of the pt1 member of screen.

TCPL/6.1_Basics_of_Structures (2008-02-23 15:34:08由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.