Static initalization of global variable?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sat Mar 3 16:56:18 PST 2007


Rick Mann wrote:
> I keep getting bitten by things that seem obvious. I'm trying to do this:
> 
> import ...CFString;
> 
> CFStringRef kHIAboutBoxNameKey;
> 
> static
> {
> 	kHIAboutBoxNameKey              =	CFSTR("HIAboutBoxName");
> }
> 
> 
> But GDC 0.22 complains with: "no identifier for declarator kHIAboutBoxNameKey".
> 
> I don't even know what this is telling me. I basically want a variable to hold a (constant) value that other code can use. In this case, CFStringRef is a pointer to a struct type declared in CFString. What must I do?

The static {} block isn't something that gets executed (as it would be 
in e.g. Java), it just means that all declarations in it will have the 
attribute "static". What you put into it isn't a declaration, so that's 
what it complains about. Your options:
---
CFStringRef kHIAboutBoxNameKey = CFSTR("HIAboutBoxName");
---
(if CFSTR() can be evaluated at compile time)

or
---
CFStringRef kHIAboutBoxNameKey;

static this()
{
	kHIAboutBoxNameKey              =	CFSTR("HIAboutBoxName");
}
---
(where CFSTR() gets run at the start of the program)


More information about the Digitalmars-d-learn mailing list