C ++: How to create a utility class?

But I don’t know if I should look for static methods, just a header, a class or something else?

What would be best practice? But I do not want to have an instance of the utility class.

I want to add features like:

Uint32 MapRGB (int r, int g, int b); const char* CopyString(const char* char); // etc. You know: utility methods... 
+5
c ++
Jun 18 '10 at 15:09
source share
5 answers

Do not put them in a class; just make them non-member functions in the namespace area.

There is no rule that every function should be a member function of any class.

+21
Jun 18 '10 at 15:11
source share

One of the factors is whether it's even worth putting them in a class or just putting them as numbers in a namespace (in Java, you have to use a class, but C ++ offers namespaces).

If you make it a member of the class, then the most important decision that you must solve for each function is whether it will require or should affect any state that is not received or passed through its parameters and the return value. If this is not the case, then this should be done static , since you will not use the hidden argument "this".

One argument for using a class instead of a namespace is if your utility class may need additional methods to implement it (for example, in the case of recursion, complex calculations, etc.). Then you can make your static public method and all that it is implemented on top of static private . It has been many years since I used C ++, but I don’t think you can "hide" functions other than members in the namespace (someone corrects me if I am wrong).

From the point of view of designing a function interface, consider the number of arguments. If there are too many input arguments (especially if they have similar types, and some are related), you may want to use additional types instead of passing multiple arguments. For example, instead of calculateVolume(int x, int y, int z) you can do something like calculateVolume(Point3D) . Similarly, in your case, use the RGB class. This may sound silly, but it can save some unpleasant errors (for example, if you have functions that accept ints and RGB), and time (if you need to pass values ​​to other functions). You can create a static factory method to make it easier to create these types when passing arguments. For example: doSomethingWithColor(RGB.create(20,30,40))

+3
Jun 18 '10 at 15:13
source share

There is probably no reason to have a class to port these functions if they do not logically belong to the class. In this case, you can just free them. It might be advisable to keep them in the namespace in order to avoid name conflicts.

If you want to provide strong logical grouping of classes, there is no real harm in being static member functions of the class, but I see no reason why you should have functions like MapRGB() and CopyString() should be members of the same class.

+1
Jun 18 '10 at 15:12
source share

If you just want to group functions together, but not create an instance of the group, then you should probably think about putting them in the namespace, and not in the class at all.

I would try to use meaningful namespaces, though, given that MapRGB and CopySstring together make little sense. If you really need both, and don't really have any other functions related to strings or RGB matching, including them in the "utillity" namespace may make sense, but if you use them, it seems that you have some more string "substance" and some more "color" materials, and probably they can have a namespace for each.

+1
Jun 18 '10 at 15:14
source share

I usually have a .a (.lib on windows) called "util" that is bundled. Usually, however, the util class is bad news, and it introduces a dependency that violates the standard object-oriented design. Now, if you try to pull out a class for reuse in another project, you have thousands of hidden dependencies on your utility library. In principle, this is useful, but do your best not to put things there.

+1
Jun 18 '10 at 15:15
source share