Initialization of a local static variable with a member

BCS ao at pathlink.com
Sun Nov 11 09:34:16 PST 2007


Reply to Janice,

> On 11/11/07, Luke <kazade at gmail.com> wrote:
> 
>> Hi all,
>> 
>> I've just started learning D (I've been programming C++ for 7 years).
>> While I was converting some old C++ code to D I've stumbled across
>> this problem:
>> 
>> float GetFPS(uint elapsedFrames = 1) {
>> static LARGE_INTEGER s_lastTime = m_startTime; //< this line
>> This gives "ERROR: non-constant expression this.m_startTime". Is
>> there any way to avoid this error?
>> 
> Actually, what you're trying to do seems strange. m_startTime is a
> member variable. GetFPS is a member function. So, what are you doing
> using a "local static" (i.e. GLOBAL) variable at all? For a start,
> it's not thread-safe.
> 
> In this particular example, I'd be inclined to make lastTime a private
> class member variable. As in:
> 
> private LARGE_INTEGER lastTime;
> float GetFPS(uint elapsedFrames = 1) {
> lastTime = startTime;
> }


assuming that adding it to the class adds to much memory and that you are 
assuming that only one "frame generation engine" will be running

float GetFPS(uint elapsedFrames = 1) {
startic LARGE_INTEGER lastTime = BeforeEverything;
lastTime = startTime;
}

OTOH I think Janice has a point.





More information about the Digitalmars-d mailing list