www.digitalmars.com [Home] [Search] [D]
Last modified Feb 28, 2004.

Structs, Unions, Enums

Structs, Unions

	AggregateDeclaration:
		Tag { DeclDefs }
		Tag Identifier { DeclDefs }
		Tag Identifier ;

	Tag:
		struct
		union
	
They work like they do in C, with the following exceptions: Structs and unions are meant as simple aggregations of data, or as a way to paint a data structure over hardware or an external type. External types can be defined by the operating system API, or by a file format. Object oriented features are provided with the class data type.

Static Initialization of Structs

Static struct members are by default initialized to whatever the default initializer for the member is, and if none supplied, to the default initializer for the member's type. If a static initializer is supplied, the members are initialized by the member name, colon, expression syntax. The members may be initialized in any order. Members not specified in the initializer list are default initialized.
	struct X { int a; int b; int c; int d = 7;}
	static X x = { a:1, b:2};	// c is set to 0, d to 7
	static X z = { c:4, b:5, a:2 , d:5};	  // z.a = 2, z.b = 5, z.c = 4, z.d = 5
	
C-style initialization, based on the order of the members in the struct declaration, is also supported:
	static X q = { 1, 2 };	  // q.a = 1, q.b = 2, q.c = 0, q.d = 7
	

Static Initialization of Unions

Unions are initialized explicitly.
	union U { int a; double b; }
	static U u = { b : 5.0 };		// u.b = 5.0
	
Other members of the union that overlay the initializer, but occupy more storage, have the extra storage initialized to zero.

Struct Properties

	.sizeof			Size in bytes of struct
	.size			Same as .sizeof
	.alignof		Size boundary struct needs to be aligned on

Enums

	EnumDeclaration:
		enum identifier { EnumMembers }
		enum { EnumMembers }
		enum identifier ;

	EnumMembers:
		EnumMember
		EnumMember ,
		EnumMember , EnumMembers

	EnumMember:
		Identifier
		Identifier = Expression
	
Enums replace the usual C use of #define macros to define constants. Enums can be either anonymous, in which case they simply define integral constants, or they can be named, in which case they introduce a new type.
	enum { A, B, C }	// anonymous enum
	
Defines the constants A=0, B=1, C=2 in a manner equivalent to:
	const int A = 0;
	const int B = 1;
	const int C = 2;
	
Whereas:
	enum X { A, B, C }	// named enum
	
Define a new type X which has values X.A=0, X.B=1, X.C=2

Named enum members can be implicitly cast to integral types, but integral types cannot be implicitly cast to an enum type.

Enums must have at least one member.

If an Expression is supplied for an enum member, the value of the member is set to the result of the Expression. The Expression must be resolvable at compile time. Subsequent enum members with no Expression are set to the value of the previous member plus one:

	enum { A, B = 5+7, C, D = 8, E }
	
Sets A=0, B=12, C=13, D=8, and E=9.

Enum Properties

	.min			Smallest value of enum
	.max			Largest value of enum
	.sizeof			Size of storage for an enumerated value

	For example:

	X.min			is X.A
	X.max			is X.C
	X.sizeof		is same as int.sizeof

Initialization of Enums

In the absence of an explicit initializer, an enum variable is initialized to the first enum value.
	enum X { A=3, B, C }
	X x;		// x is initialized to 3
	

Copyright (c) 1999-2004 by Digital Mars, All Rights Reserved