Variant Records: The equivalent to the C-union structure

原创 2004年07月21日 22:07:00
Variant Records: The equivalent to the C-union structure

Is there a way to create a C 'union'-like structure in Delphi? That is, a structure that uses the same memory area?

The Delphi (Pascal/ObjectPascal) equivalent to a C-union structure is called a Variant Record (not to be confused with the Variant "type" available in Delphi 2.0+). As with a C-union, the Pascal variant record allows several structure types to be combined into one, and all will occupy the same memory space. Look up the syntax declaration under "Records" in the help file. But here's an example:

 

TPerson = record
 FirstName,
 LastName: string[40];
 BirthDate: TDate;
 case Citizen: Boolean of
  True: (BirthPlace: string[40]);
  False: (Country: string[20];
   EntryPort: string[20];
   EntryDate: TDate;
   ExitDate: TDate);
end;

The record above is actually a single expression of two records that could describe a person:

type
TPersonCitizen = record
 FirstName,
 LastName: string[40];
 BirthDate: TDate;
 BirthPlace: string[40]
end;

and

type
TPersonAlien = record
 FirstName,
 LastName: string[40];
 BirthDate: TDate;
 Country: string[20];
 EntryPort: string[20];
 EntryDate: TDate;
 ExitDate: TDate;
end;

And as in a union, the combination of the two types of records makes for much more efficient programming, because a person could be expressed in a variety of ways.

Everything I explained above is pretty hypothetical stuff. In Delphi, the TRect structure that describes a rectangle is actually a variant record:

type
TPoint = record
 X: Longint;
 Y: Longint;
end;

TRect = record
 case Integer of
  0: (Left, Top, Right, Bottom: Integer);
  1: (TopLeft, BottomRight: TPoint);
end;

where the coordinates of the rectangle can be expressed using either four integer values or two TPoints.

I realize this is pretty quick and dirty, so I suggest you refer to the help file for a more in-depth explanation, or go to your nearest book store or library and look at any Pascal book (not Delphi -- most won't explain this fairly esoteric structure). However, if you're familiar with the C-union, this stuff should be an absolute breeze.

datalab 深入理解计算机系统实验

是关于位操作的实验,总体上比较简单。个人感觉bitcount比较难,我参考了stackoverflow中的一篇帖子,讲的挺好。网址是http://stackoverflow.com/questions...
  • lwfcgz
  • lwfcgz
  • 2013年01月17日 21:09
  • 31556

CSAPP深入理解计算机系统实验datalab解析

看完这一本《CSAPP深入理解计算机系统》
  • u014124795
  • u014124795
  • 2014年08月10日 18:00
  • 7858

数据结构《14》----并查集 Union-Find

描述:     并查集是一种描述解决等价关系。能够方便地描述不相交的多个集合。     支持如下操作    1. 建立包含元素 x 的集合  MakeSet(x)                   ...
  • u012653791
  • u012653791
  • 2014年02月26日 19:51
  • 1659

Penalty Records - GROUP BY; HAVING; UNION;

Query for yellow card: with a given Group, so its id is given. with a given Event name, so its...
  • zlxadhkust
  • zlxadhkust
  • 2011年10月27日 22:58
  • 366

Union-Find: A Data Structure for Disjoint Set Operations

  • 2010年06月20日 22:02
  • 608KB
  • 下载

从Java到C++——从union到VARIANT与CComVariant的深层剖析

我第一次用VARIANT和CComVariant的时候完全不明白它是怎么回事,为它什么数据类型都可以存放,什么数据都可以被可以初始化?里面到底是怎么实现的?感觉又神奇又迷惑!我们在上一节中讲了unio...
  • luoweifu
  • luoweifu
  • 2014年06月29日 17:57
  • 2528

Oracle Databases, a Penetration Tester’s View of Unauthorized Access to Customer Records

When penetration-testing you get to see lots of seemingly unbelievable security failures, but they’r...
  • cnbird2008
  • cnbird2008
  • 2012年06月01日 11:26
  • 639

Writing audit records to Windows Event Log failed

ORA-28056:Writing audit records to Windows Event Log failed 今天同事遇到这个问题。表面现象是,odbc无法连接。 不过我检查问题...
  • bianyalibianyali
  • bianyalibianyali
  • 2011年12月28日 08:14
  • 594

ADF: The best way to indicate for records in a table the existence of details

An interesting discussion arose in one of the projects I am involved in. The functional requirement ...
  • luyushuang
  • luyushuang
  • 2011年10月31日 09:22
  • 994

A class to call stored procedures that do not return records

  • 2006年02月23日 09:05
  • 8KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Variant Records: The equivalent to the C-union structure
举报原因:
原因补充:

(最多只允许输入30个字)