digitalmars.D.learn - Static initalization of global variable?
- Rick Mann (11/11) Mar 03 2007 I keep getting bitten by things that seem obvious. I'm trying to do this...
- Frits van Bommel (18/33) Mar 03 2007 The static {} block isn't something that gets executed (as it would be
- Rick Mann (3/12) Mar 09 2007 Ah! My bad. I meant to do static this(). Stupid Java habits.
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
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
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