473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Encapsulating related utility functions

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

Please site articles or books if possible.
Jun 27 '08 #1
6 2221
On 2008-06-22 16:22, Marco wrote:
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

Please site articles or books if possible.
Ask your self this (or ask your collogues):

* If you will never instantiate it why use a class, or put another way:
how is it object-oriented to create a class but never create any
objects from it?

* If you create a class with static functions, what relation does these
functions have with the class (except that the class provides a
namespace for the functions)?

--
Erik Wikström
Jun 27 '08 #2
Erik Wikström wrote:
On 2008-06-22 16:22, Marco wrote:
>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

Please site articles or books if possible.

Ask your self this (or ask your collogues):

* If you will never instantiate it why use a class, or put another way:
how is it object-oriented to create a class but never create any
objects from it?
And what's wrong with it not being OO?

* If you create a class with static functions, what relation does these
functions have with the class (except that the class provides a
namespace for the functions)?
In the case of the OP, probably none; but then again: why would that be bad?

One difference between a class and a namespace is that you (or anybody else)
can add stuff to a namespace whereas a class is closed.

A more interesting difference is that a class can be used as a template type
parameter. That can make option (1) appropriate in some cases. The class
can provide implementations (and sometimes even semantics) for primitive
operations used in an algorithm. (E.g., one could have a string class that
takes a policy class supplying implementations of search and pattern
matching algorithms; or you could have a vector whose reallocation strategy
is governed by a policy. Changing the policy would be a way to optimize a
program.)
Now, to evaluate the options (1) and (2) on their merits, I still agree that
if those template tricks don't enter the picture, option (2) is the way to
go. But that is simply because I don't see why you would want to close the
namespace.

Best

Kai-Uwe Bux
Jun 27 '08 #3
On Jun 22, 4:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-06-22 16:22, Marco wrote:
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
Please site articles or books if possible.
Ask your self this (or ask your collogues):
* If you will never instantiate it why use a class, or put another way:
how is it object-oriented to create a class but never create any
objects from it?
To close it? A class differs from a namespace in several ways.
One, of course, is that you can't instantiate a namespace.
Another is that you can't add names to a class. Yet a third is
that a class can have private members.

Obviously (I think), none of these issues apply to additional
math functions, and these should probably be in a namespace.
But there are probably cases where closure or private members
are an issue, in which case, a class should be used.
* If you create a class with static functions, what relation
does these functions have with the class (except that the
class provides a namespace for the functions)?
If the functions are private, they can only be called by other
members of the class.

Note that I'm just playing the devil's advocate here. Generally
speaking, the original question can be restated to ask "is it
appropriate to confuse the reader by pretending something is OO
when you're not?"

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
Hi!

Erik Wikström wrote:
Ask your self this (or ask your collogues):

* If you will never instantiate it why use a class, or put another way:
how is it object-oriented to create a class but never create any
objects from it?

* If you create a class with static functions, what relation does these
functions have with the class (except that the class provides a
namespace for the functions)?
There are meaningful semantics of classes with only static members.

For example something like that:

template<class T, class K>
class instance_repository
{public:
// abstract factory interface used for object instantiation
struct IFactory
{ virtual T* operator()(K& key) = 0;
};

public:
// Get an existing instance of T or return NULL.
static intrusive_ptr<TFindByKey(const K&);
// Get an existing instance of T or create a new one.
static intrusive_ptr<TGetByKey(K& key, IFactory& factory);
private:
static sorted_vector<T*, const K&Index;
static mutex Mtx; // protect the index above
};
Marcel
Jun 27 '08 #5
Marco wrote:
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

Please site articles or books if possible.
Use a named namespace. The effect is the same (and the accessing syntax
will likely be the same). The benefit of using a namespace is that
namespaces are open, and you can have several header files which "add"
to the namespace.

For example, say you have functions for trig, and functions for algebra.
It might make sense to put them all in the namespace my_math_space, but
have "trig.h" and "algebra.h". You couldn't do that that a class.

Classes should, in general, be used to encapsulate state/value. If you
have no state or value, then a class doesn't make sense. Note, type is
part of state, so polymorphic classes without data members do make sense
as classes.

Hope this helps,
Daniel.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #6
On Jun 22, 7:22 am, Marco <prenom_no...@yahoo.comwrote:
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

site
oops that should be: Please cite articles or books if possible.
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Paul Moore | last post by:
I was just writing some code which did date/time manipulations, and I found that the Python 2.3 datetime module does not supply a number of fairly basic functions. I understand the reasoning (the...
0
by: Carlo Milanesi | last post by:
Let's say I want to write the following function, void f(const list<int> &l, const vector<int> &v) { cout << get_second(l) << '\n'; cout << get_second(v) << '\n'; cout << get_nth(l, 2) << '\n';...
5
by: Neil Zanella | last post by:
Hello, I need to access some user defined utility functions from within my ASP.NET pages. I wonder whether there is a way to do this. I do not want to use inheritance. I just want to be able to...
22
by: John Salerno | last post by:
I might be missing something obvious here, but I decided to experiment with writing a program that involves a class, so I'm somewhat new to this in Python. Anyway, what is the best way to create...
25
by: John Salerno | last post by:
Just a quickie for today: Is it common (and also preferred, which are two different things!) to create a function that has the sole job of calling another function? Example: for fun and...
8
by: alessandro menchini | last post by:
Hi all, When i try to drop a table, and this table have (for example) some function related...db2 don't allow to drop the table how can i enter a command to drop the table AND all the objects...
4
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. Currently file-directory-related functionality in the Python standard library is scattered among various modules such as shutil, os,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.