Re: Static Variables and Multiple Inheritance
On Aug 11, 9:05 am, Jaco Naude <naude.j...@gmail.com> wrote:
Hi
I am trying to think of an efficient way of implementing a logging
system for a big program. My thoughts are to create a message logging
base class and letting all objects that need logging ability inherit
from this base class along with the other base classes they inherit
from. This should work (not sure how efficient it is though) but I
want to share the data structure that keeps the log messages between
all the classes that's inherited from this base class. (This data
structure will either be written to the disk, displayed using message
boxes or added to a tree view type of class in the main GUI). To do
this, I am thinking to declare the data structure as static, but I'm
not sure if this will do the trick...
Any idea if this will work? If not, a global data structure can
probably do the trick I don't want to do that. Is there maybe a
simpler way of doing this type of thing?
Using inheritance may work, but it doesn't follow the "is-a" vs. "has-
a" OO design principle. That is, your classes are not kinds of
loggers; they use a logger. Inheritance represents the former, not the
latter (except for non-public inheritance, but still it's somewhat
misleading to implement "has-a" this way IMHO). Hence, you should
probably either pass a logger instance in via the constructor, which
could be passed all the way to the base class, or use a global,
possibly a singleton. See the chapter on singletons in Alexandrescu's
_Modern C++ Design_ where he dwells on how to implement loggers with
various feature sets, and see the most updated code from that book
here:
http://loki-lib.sourceforge.net/
Cheers! --M