473,803 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2253
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 objektorientier ter Datenverarbeitu ng
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_reposi tory
{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<T FindByKey(const K&);
// Get an existing instance of T or create a new one.
static intrusive_ptr<T GetByKey(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...@y ahoo.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
6442
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 datetime module provides a set of datatypes, and doesn't attempt to get into the murky waters of date algorithms) but as these things can be quite tricky to get right, I was wondering if anyone has already implemented any date algorithms, or...
0
2350
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'; cout << get_nth(v, 2) << '\n'; } that prints the second and third (counting from zero) items of given
5
1883
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 call some code contained in a .cs file (C# file) from within several ..aspx and .ascx page without having to rewrite the code in each such page. I would like to know how this can be accomplished, including how I can ensure that ASP.NET will find...
22
5361
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 a function (A) within a class that another function (B) can use? Function A is not something that an instance will ever call, so I figure it's a choice between static or class methods, but I don't know which one, or if this is even the right...
25
1874
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 exercise, I'm creating a program that takes a quote and converts it into a cryptogram. Right now I have four functions: convert_quote -- the main function that starts it all make_code -- makes and returns the cryptogram make_set -- called from...
8
6641
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 (functions...triggers...views) that depends on that table? A.M.
4
1429
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, dircache etc. So I request that the functions be gathered and consolidated at one place. Some may need renaming to avoid conflicts or for clarification.
0
9703
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
9565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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
9125
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
7604
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
6844
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
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.