473,809 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Utility classes?

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
7 2275
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

"Bora" <no****@nospam. none> wrote in message
news:bq******** **@fred.mathwor ks.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
"Bora" <no****@nospam. none> wrote in message news:<bq******* ***@fred.mathwo rks.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::DoStuf f();

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
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.l earn.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

"Bora" <no****@nospam. none> wrote in message
news:bq******** **@fred.mathwor ks.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
"Bora" <no****@nospam. none> wrote in message news:<bq******* ***@fred.mathwo rks.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**********@br egmasoft.com
Jul 22 '05 #7
"Bora" <no****@nospam. none> wrote in message news:<bq******* ***@fred.mathwo rks.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1520
by: Zane Thomas [.NET/C# MVP] | last post by:
If, like me, your connection is being swamped downloading copies of the Swen Worm and bounce messages, you might find the utility I wrote today useful. Source for the utility is distributed with the Abderaware Mail for .NET demo package. Later (tonight?) I'll _may_, time permitting, make an executable only with the registered version of the components so that the hourly nag-screen does not have to be tolerated. After installation the...
0
1595
by: james | last post by:
My fellow VB.NET developers, Ever wonder why MS didn't include a "Class Builder" in Visual Studio .NET like they did in other versions of visual studio? So did we.
1
1311
by: Dhanak | last post by:
I have around 1000 aspx files that contains standard lines intended for debugging purpose.Now before we put these to production we need to remove these lines from all files. Instead of doing it manually, we would like to develop a small utility that can do this. In a prototype of that utility, We r currently using StreamReader and Regex Classes of .Net to do this. Can somebody suggest any other solution/Logics which can improve...
2
1894
by: Christoph Haas | last post by:
Evening, I'm currently working on a larger Python project that consists of multiple programs and packages. As I need a few utility functions time and again I moved them all into a Utility package and created a class there. Like this: Util.py: ~~~~~~~~ class Util: def __init__(self, debugFlag=False, vibranceLevel='good'):
7
3217
by: wolfing1 | last post by:
Is there a way to see what CSS classes in a page are not defined? (like if I want to find mispelled class names, etc)
27
2160
by: Neil | last post by:
Anyone have a recommendation for a good inexpensive or free utility for developing table schemas.
4
3227
by: Jeff User | last post by:
Hi all Developing C# web apps, .net1.1 I have gotten in habit of placing commonly used (interface) functions in my base page. However, some apps I work on use a seperate "Utility" class containing common utility functions. Is there any benefit in doing it one way or the other? For instance, does having to load up a seperate class cost more or
6
2255
by: Marco | last post by:
One of the things I like about C++ is that it doesn't force you to create fake "objects" in the OO sense. We had a debate at the office about the "right" way to encapsulate related utility functions (such as additional math functions). These would be pure functions with no state. 1) Create a class with all static functions 2) Use a named namespace
37
2629
by: Phlip | last post by:
1230987za wrote: Kanze is a classically-trained "unit tester". In some circles "unit" is a QA concept - specifically, if a test fails, you only need to inspect one unit. So "units" are "things which are clearly delimited and accessible to inspection". That should map onto C++ classes - specifically due to overlapping requirements. C++ classes _should_ be "things which are clearly delimited and accessible to inspection". Yet...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.