I should have provided more information.
-----base class---
struct IUtil{
virtual void print(){}};
----end-----
----injection---
class Util : public IUtil{
public:
void print(){...}
};
class Work{
void SetUtil(IUtil* pUtil){m_pUtil = pUtil;}
IUtil* m_pUtil;};
----end----
----static----
class Util{
public:
static IUtil* s_pUtil;};
IUtil* Util::s_pUtil = NULL; //Util::s_pUtil = pUtil; (do assignment
somewhere else.)
----------
From the above code you can see that both provides flexibility. The
only difference is the implementation methods.
So any essential difference?
I'll presume that Work class has e.g. this:
void Work::work() {... m_pUtil->print(); ...}
If all instances of Work should use same Util, then clearly m_pUtil is
overkill. If not, then it isn't. But we don't know any of that, so we
can't tell you. It is very hard to guess these things, and it is very
hard to predict the future.
If, for example, at this stage, you use one Util for all Work-s, but
think that you might need separate ones later, you can do e.g.:
class Work
{
....
Util* GetUtil() { return GetGlobalUtil(); }
....
};
and not use m_pUtil anymore. Then, once you realize that you do want
several Util-s, just add your SetUtil and change GetUtil to:
Util* GetUtil() { return m_pUtil; }
That way, you get simpler design, no runtime cost, and the ability to
change later with less actual code changes.
Goran.