www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static initalization of global variable?

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
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?

TIA,
Rick
Mar 03 2007
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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)
Mar 03 2007
prev sibling parent Rick Mann <rmann-d-lang latencyzero.com> writes:
Frits van Bommel Wrote:

 Your options:
 ---
 CFStringRef kHIAboutBoxNameKey;
 
 static this()
 {
 	kHIAboutBoxNameKey              =	CFSTR("HIAboutBoxName");
 }
 ---
Ah! My bad. I meant to do static this(). Stupid Java habits. Thanks!
Mar 09 2007