Re: Utility classes?
From: Noah Roberts (nroberts_at_dontemailme.com)
Date: 12/05/03
- Next message: Hongzheng Wang: "Re: VC++ CListCtrl question."
- Previous message: Joseph Smith: "VC++ CListCtrl question."
- In reply to: Bora: "Utility classes?"
- Next in thread: Chris Theis: "Re: Utility classes?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 05 Dec 2003 00:46:54 -0800
Bora wrote:
> I usually find that a number of unrelated classes require the same kind of
> operations. However, I don't want to duplicate code in multiple places. In
> Java, I've seen those "Utility Classes", which are basically static
> (singleton) classes created to encapsulate related operations in one class.
> These methods can be called from other classes that need same functionaliy.
>
> Is there a similar concept in C++?
You can use namespaces and just regular functions, or you can create a
singleton that contains these functions. Really depends on what your
needs are.
>
> I know that if two classes require the same operations, it might be
> reasonable to derive them from a base class and move those operations to the
> base class. However, in my case, I really considered this and found that my
> classes represent really different concepts, albeit requiring a number of
> similar operations. So, creating a utility class seemed reasonable.
C++ allows for multiple inheritance but in my opinion it should be
avoided. There are many cases where it becomes necissary to use MI,
like for instance when using the observer pattern, but short of these
necesities MI should not be used. In my opinion, and I am not alone, it
is better to repeat code than to create a messy inheritance tree through
suplerfuous use of MI.
Unless you need some special stuff I would recommend using namespaces.
Namespaces will encapsulate your functions without the weight of having
a class, not that it is that much anyway.
NR
- Next message: Hongzheng Wang: "Re: VC++ CListCtrl question."
- Previous message: Joseph Smith: "VC++ CListCtrl question."
- In reply to: Bora: "Utility classes?"
- Next in thread: Chris Theis: "Re: Utility classes?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|