c++ utility class

J

Jim Strathmeyer

So, right now I have a class that is basically utility functions; it's
methods are all static because there is no class data, and I would never
create an object of this class. So I call these functions like
'Utility::Example()'. Is this a common way to do this? Or should I just
create a file of funcitons like I would in C?
 
J

Jerry Coffin

Jim said:
So, right now I have a class that is basically utility functions; it's
methods are all static because there is no class data, and I would never
create an object of this class. So I call these functions like
'Utility::Example()'. Is this a common way to do this? Or should I just
create a file of funcitons like I would in C?

It sounds a great deal as if what you're looking for is a namespace.
 
D

davidrubin

Jim said:
So, right now I have a class that is basically utility functions; it's
methods are all static because there is no class data, and I would never
create an object of this class. So I call these functions like
'Utility::Example()'. Is this a common way to do this? Or should I just
create a file of funcitons like I would in C?

This is exactly what you want to do, although I would use a struct
rather than a class. The reason you don't want to use a namespace is
because namespaces are "open", meaning that functions (for example) can
be added to the namespace from a different component. This breaks the
encapsulation and safety you are trying to provide.
 
J

Jim Langston

Jim Strathmeyer said:
So, right now I have a class that is basically utility functions; it's
methods are all static because there is no class data, and I would never
create an object of this class. So I call these functions like
'Utility::Example()'. Is this a common way to do this? Or should I just
create a file of funcitons like I would in C?

Yes. This is a common way to do this.
 
I

Ian Collins

This is exactly what you want to do, although I would use a struct
rather than a class. The reason you don't want to use a namespace is
because namespaces are "open", meaning that functions (for example) can
be added to the namespace from a different component. This breaks the
encapsulation and safety you are trying to provide.
He doesn't specify closure, only a collection.

So if he wants the collection to be extensible, use a namespace, if
closed, use a struct.
 
D

Dietmar Kuehl

Jim said:
So, right now I have a class that is basically utility functions; it's
methods are all static because there is no class data, and I would never
create an object of this class. So I call these functions like
'Utility::Example()'. Is this a common way to do this?

No, this is not done in C++. Only programmers of so-called "object
oriented" languages or people believing that object orientation is
goal rather than a tool do this.
Or should I just
create a file of funcitons like I would in C?

Neither is this the C++ way. The C++ way is to use a namespace:

namespace Utility
{
void Example();
// ...
}
 
H

Howard

Jim Langston said:
Yes. This is a common way to do this.

I can't imagine why I'd want to define a class just as a collecion point for
functions which are not related to any kind of state data. What purpose
does the class serve?

A namespace would seem the more logical approach if all you want is to
gather the functions together under some kind of grouping.

-Howard
 
R

red floyd

Howard said:
I can't imagine why I'd want to define a class just as a collecion point for
functions which are not related to any kind of state data. What purpose
does the class serve?

A namespace would seem the more logical approach if all you want is to
gather the functions together under some kind of grouping.

Legacy (pre-Standard) code for one. Before namespaces were introduced,
a class with static member functions only made sense for that purpose.

Templateized traits classes?

template <typename T>
struct some_kind_of_traits {
static bool has_some_trait();
static int some_other_trait_value();
// etc...
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top