Static Variables and Multiple Inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jaco Naude

    Static Variables and Multiple Inheritance

    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?

    Thanks in advance
    Jaco
  • mlimber

    #2
    Re: Static Variables and Multiple Inheritance

    On Aug 11, 9:05 am, Jaco Naude <naude.j...@gma il.comwrote:
    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:

    Download Loki for free. A C++ library of designs, containing flexible implementations of common design patterns and idioms.


    Cheers! --M

    Comment

    • tomisarobot@gmail.com

      #3
      Re: Static Variables and Multiple Inheritance

      Global access around a structure is usually the most convenient, but
      nothing is really great for logging in OO systems. Allow your design
      multiple logs accessed, you generally end up wanting it at some
      point. If you want one default, just overload with a literal.

      namespace log {
      template<typena me T>
      Log(const std::string &whichLog, const T &t);
      }

      std::stringstre am is your friend for impl.

      Comment

      • James Kanze

        #4
        Re: Static Variables and Multiple Inheritance

        On Aug 11, 3:05 pm, Jaco Naude <naude.j...@gma il.comwrote:
        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...
        It doesn't really make sense, any more than having every class
        inherit from a class Object makes sense. Everything is, or
        should be "loggable" (including things like int, which can't
        derive from a LoggableObject class).
        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?
        Logging is a global service, which means at some level, you
        always end up with some sort of "global" object. Usually
        several, in fact.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...