By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

Utility classes?

P: n/a
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++?

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.

I'd like to here your suggestions about this.

Thanks,
Bora
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a

"Bora" <no****@nospam.none> wrote in message
news:bq**********@fred.mathworks.com...
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++?

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.


Well, if you give people a hammer they'll certainly find a nail. Inheritance
is a good & clever concept but it should be reserved to things that are
related to each other. Thus inheriting only to share some implementation is
not the right way IMHO. There are numerous discussions about when to inherit
and what to inherit (see Effective C++ by Scott Meyers, Exceptional C++ by
Herb Sutter and so on....). What you can do is that you either implement the
functionality in terms of free functions or put them into a class as static
functions. For example

class CMath {
public:
static double Pi() { return 4.0 * atan(1.0); };
static double SinSquared( double x ) { return sin(x) * sin(x); };
};

HTH
Chris
Jul 22 '05 #3

P: n/a
"Bora" <no****@nospam.none> wrote in message news:<bq**********@fred.mathworks.com>...
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++?
certanly:

Class MyUtils
{
public:
static void DoStuff();
static void DoMoreStuff();
};

that's a class with only static members which serves as a utility
class. you never need to create instances of this class since you
never need them since all the methods are static. this is not a
singleton in the conventional way of being a singleton since there
isn't even one instance of this class. to call methods of this class
you do:
MyUtils::DoStuff();

you can even enforce the fact that there should be no instances
created doing the following:

Class MyUtils
{
public:
static void DoStuff();

private:
MyUtils(); // private constructor with NO implementation in the cpp.
}

this will cause a compiler error to whoever trys to instantiate this
class.
good luck.

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.

I'd like to here your suggestions about this.

Thanks,
Bora

Jul 22 '05 #4

P: n/a
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++?

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.

I'd like to here your suggestions about this.

Thanks,
Bora


Check out the Boost library (http://www.boost.org)
especially the package for arithmetic operators which provides a
template for adding those pesky ==, !=, <, >, <=, >= operators
will reduced annoyance.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5

P: n/a

"Bora" <no****@nospam.none> wrote in message
news:bq**********@fred.mathworks.com...
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++?

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.


You might want to do a search on "mix in" or "mixin" classes.
Jul 22 '05 #6

P: n/a
"Bora" <no****@nospam.none> wrote in message news:<bq**********@fred.mathworks.com>...
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++?

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.

I'd like to here your suggestions about this.


What you've encountered in Java a an attempt to work around the
limitations of the language to provide necessary functionality. Such
a workaround is not necessary in C++.

If you find you have operations on data that are not specific to those
data, then you implement that as namespace-level functions. You may
find you need to template those functions to handle different types
differently, and specialize those templates for special cases.

A good example of how this is done is the standard library that comes
with C++. It contains a slew of algorithms that operate on data.
They aren't utility classes, they're functions.

The concept of a class in C++ is simpler than in Java. In C++ a class
is a collection of related data and functions that operate on those
data. If you have functions that don't operate on the data contained
in a class, why should those functions be contained in the class?

--
Stephen M. Webb
st**********@bregmasoft.com
Jul 22 '05 #7

P: n/a
"Bora" <no****@nospam.none> wrote in message news:<bq**********@fred.mathworks.com>...
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++?

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.


There are many options, and it depends on the context how you handle it.

For example, if the utility functionality does not require any access
to the data members of a class, it may be appropriate to put it in a
global function. The archetype there is numerical functions like sin,
exp, and so on. You pass them a single arg, or possibly a very small
number of args, and they send back the result. This is also most
reasonable when you don't want any retention of data from one call
to the next. You might include them in a class if they needed to have
retention of data. In that case, they could either be static or
non-static, depending on if you need independent sequences of calls.

If you are looking for something that handles generic classes, you
should consider a template. It is often possible to implement an
algorithm in a template, then pass it any class to which the
algorithm makes sense. There will likely still be some overhead on
the classes that are processed this way. For examples, look at
the standard library algorithm templates.
Socks
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.