www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Initialization of a local static variable with a member

reply Luke <kazade gmail.com> writes:
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?
Nov 11 2007
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 11/11/07, Luke <kazade 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; }
Nov 11 2007
parent BCS <ao pathlink.com> writes:
Reply to Janice,

 On 11/11/07, Luke <kazade 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.
Nov 11 2007