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))
Uri Jun 18 '10 at 15:13 2010-06-18 15:13
source share