vivekian said:
Writing a small simulator which schedules tasks. In this case the
pointer to the current running task descriptor is the global variable
and should be visible across files. So as mentioned above i would have
something like
class task
{
...
} ;
extern task * current ;
Is making it global a good choice ? or is static fine ( have my doubts
though since the variable is too be present across files ?
A better design might be:
class task
{
private:
task * current;
public:
static task& current_task() { return *current; } // there better be
one too...
};
Now, you could make that code more robust by making sure there IS a
current task but the point is that no global is required and the class
itself keeps track of the things it should be tracking. One major
benefit is that you can place the checks to see if there is a current
task in ONE place instead of all over the damn place (any time you
access the global pointer you would need to check).
You could also have some sort of "task manager" class that maybe
contains all the tasks to be performed and manages them in some
way...it might be a better place to track the _current_ task. Your
task manager could very well be a singleton.