Skip to main content
gamedev.net
🔒 Locked

static member variable

Started by Matheass May 4, 2004 at 2:19 PM 7 replies 1k views
Original Post
Matheass
Matheass
Look at my files: main.cpp
#include "Timer.h"

CTimer timer;

int WINAPI WinMani(...) {...}
Timer.cpp
   #include "Timer.h"

std::list<CTimer *> CTimer::m_List;

CTimer::CTimer()
{
     m_List.insert(m_List.begin(), this);
     ...
}
Timer.h
#include <list>

class CTimer
{
public:
     CTimer();
      ...
protected:
     static std::list<CTimer*> m_List;
};
I hope I wrote this correct; i didn't copy'n'paste; The problem is that main.cpp is linked before Timer.cpp. In main.cpp I declare one CTimer-var, so the consttructor is called. But the static list-var is still not initialised, so the m_List.include()-call failes. If I declare teh timer-var in may main function, it works correct, because teh global variables are initialised first, but sometimes, i need a global timer. [edited by - Matheass on May 4, 2004 3:22:34 PM] [edited by - Matheass on May 4, 2004 3:23:46 PM]
zaidgs
zaidgs
hope i understand you correctly,

guess you should have a static counter
that counts how many instances have been
initialized,
and in your get_time() function,
you should define a special behavior
if the counter is 0.

[edited by - zaidgs on May 4, 2004 4:00:10 PM]
Matheass
Matheass
Reply1:
I can''t just use a simple counter, because i need access to all CTimer-Objects, I have. So I used the static list
list.size() still works (I''ve tested it). But list.insert has as first argument a pointer to an element of the list before that the new element has to be inserted. (right? ) So I have put the list.head() or list.tail() there. But because the list is still not initialised the head and tail are NULL, too. so the function insert gets an NULL-pointer as argument -> bang

Now to the 2. reply:
If I use this, I have only acces for static members, isn''t it. But the most impotant things aren''t static.
Mayrel
Mayrel
I think the only thing you can do is check to see if the list has been initialised in CTimer::CTimer and do so if it hasn''t.
zaidgs
zaidgs
can you explain a little bit more whats
going wrong when an instance is global ?!?!
Aprosenf
Aprosenf
Try m_List.push_front(this); instead.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.