digitalmars.D - Initialization of a local static variable with a member
- Luke (5/5) Nov 11 2007 Hi all,
- Janice Caron (11/16) Nov 11 2007 Actually, what you're trying to do seems strange. m_startTime is a
- BCS (8/33) Nov 11 2007 assuming that adding it to the class adds to much memory and that you ar...
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
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
Reply to Janice,On 11/11/07, Luke <kazade gmail.com> wrote: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.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