您的位置:首页 > 其它

结构体扩展——Ada应用实例之五

2009-11-28 09:21 585 查看
结构体扩展——Ada应用实例之五

有一个帖子提问,是否能写个函数来改变如下结构体内的分量数目,比如添加一个char sFileName[60] ?
typedef struct {
int iKey;
int iType;
int iLen;
char sTxt[10];
}QQ_Who;
在C语言中,结构类型是静态定义的,不可能通过函数来动态地改变结构的组成。
在Ada语言中,也不可能通过函数来动态地改变结构的组成。但Ada提供了一个称为标记记录类型(tagged record type)的设施,可用于扩展记录类型。
假如我们要处理各种几何对象,这些对象构成图1 的层次结构。



图1 几何对象的层次结构

所有对象都有一个由xy坐标描述的位置,因此可以声明如下类型:
type Object is tagged
record
X_Cood: Float;
Y_Cood: Float;
end record;

这里tagged是Ada的一个保留字,它表示在运行期间该类型的值带有标记,并且该类型是可扩充的。
从类型Object可以导出其它几何对象的类型,例如:
type Circle is new Object with
record
Radius: Float;
end record;

这样,类型Circle有三个分量,其中X_Cood和Y_Cood是从类型Object继承来的,Radius是显式添加的。

参考资料:
[1] John Barnes, Programming in Ada95, 2nd edition. Addision-Wesley, 1998.

转载:Why does Ada have "tagged types" instead of classes?
http://www.faqs.org/faqs/computer-lang/Ada/programming/part2/#ixzz0Y3PvfMEo

(Tucker Taft responds): Someone recently asked me to explain the difference between the meaning of the term "class" in C++ and its meaning in Ada 9X. Here is a synopsis of the answer:
In C++, the term "class" refers to three different, but related things:
l a language construct, that encapsulates the definitions of data members, member functions, nested types, etc.;
l a particular kind of type, defined by a class construct (or by "struct" which is a special case of "class");
l a set of types consisting of a type and all of its derivatives, direct and indirect.
In Ada 9X, the term "class" refers only to the third of the above definitions. Ada 9X (and Ada 83) has three different terms for the concepts corresponding to the above three things:
l a "package" encapsulates the definitions of types, objects, operations, exceptions, etc which are logically related. (The operations of a type defined immediately within the package where the type is declared are called, in 9X, the "primitive operations" of the type, and in some sense, define the "primitive" semantics of the type, especially if it is a private type.)
l a "type" is characterized by a set of values and a set of primitive operations (there are a million definitions of "type," unfortunately, but you know what I mean...);
l a "class" is a set of types with similar values and operations; in particular, a type and and all of its derivatives, direct and indirect, represents a (derivation) class. Also, the set of integer types form the integer "class," and so on for the other language-defined classes of types in the language.
Some OOP languages take an intermediary position. In CLOS, a "class" is not an encapsulating construct (CLOS has "packages"). However, a "class" is both a type and a set of types, depending on context. (Methods "float" freely.)
The distinction Ada 9X makes between types and classes (= set of types) carries over into the semantic model, and allows some interesting capabilities not present in C++. In particular, in Ada 9X one can declare a "class-wide" object initialized by copy from a "class-wide" formal parameter, with the new object carrying over the underlying type of the actual parameter.
For example:
procedure Print_In_Bold (X : T'Class) is
Copy X, make it bold face, and then print it.
Copy_Of_X : T'Class := X;
begin
Make_Bold (Copy_Of_X);
Print (Copy_Of_X);
end P;
In C++, when you declare an object, you must specify the "exact" class of the object -- it cannot be determined by the underlying class of the initializing value. Implementing the above procedure in a general way in C++ would be slightly more tedious.
Similarly, in Ada 9X one can define an access type that designates only one specific type, or alternatively, one can define one that can designate objects of any type in a class (a "class-wide" access type). For example:
type Fancy_Window_Ptr is access Fancy_Window;
--Only points at Fancy Windows -- no derivatives allowed
type Any_Window_Ptr is access Window'Class;
-- Points at Windows, and any derivatives thereof.
In C++, all pointers/references are "class-wide" in this sense; you can't restrict them to point at only one "specific" type. In other words, C++ makes the distinction between "specific" and "class-wide" based on pointer/reference versus object/value, whereas in Ada 9X, this distinction is explicit, and corresponds to the distinction between "type" (one specific type) and "class" (set of types).
The Ada 9X approach we believe (hope ;-) gives somewhat better control over static versus dynamic binding, and is less error prone since it is type-based, rather than being based on reference vs. value.
In any case, in Ada 9X, C++, and CLOS it makes sense to talk about "class libraries," since a given library will generally consist of a set of interrelated types. In Ada 9X and CLOS, one could alternatively talk about a set of "reusable packages" and mean essentially the same thing.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: