Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am working on a C++ project where I have a bunch of math functions that I initially wrote to use as part of a class. As I've been writing more code, though, I've realized I need these math functions everywhere.

Where is the best place to put them? Let's say I have this:

class A{
        int math_function1(int);

And when I write another class, I can't (or at leat I don't know how to) use that math_function1 in that other class. Plus, I've realized some of this functions are not truly related to class A. They seemed to be at the beginning, but now I can see how they're just math functions.

What is good practice in this situation? Right now I've been copy-pasting them into the new classes, which I'm sure is the worst practice.

share|improve this question
Have you learned about the static keyword? –  S.Lott Feb 10 '12 at 18:36
In C++, free functions are almost always preferred over member functions. –  Pubby Feb 10 '12 at 18:54
There is no rule that says everything has to be in a class. At least not in C++. –  tdammers Feb 11 '12 at 9:58

3 Answers 3

up vote 32 down vote accepted

C++ can have non-method functions just fine, if they do not belong to a class don't put them in a class, just put them at global or other namespace scope

namespace special_math_functions //optional
    int math_function1(int arg)
share|improve this answer
+1 This is the most sensible solution, although the extra namespace doesn't seem necessary. –  Pubby Feb 10 '12 at 18:51
no it isn't necessary –  jk. Feb 10 '12 at 18:55
Some namespace is useful to reduce potential name collisions with other libraries. –  Bill Door Feb 10 '12 at 21:13
Using a namespace is also nice because it disambiguates whether the call is a method or a function. (math_function1(42) might call a member of the current class; special_math_functions::math_function1(42) is clearly calling an independent function). That being said, ::math_function(42) provides the same disambiguation. –  ipeet Feb 10 '12 at 21:51

Depends on how the project is organized and what sort of design patterns you are using, assuming this is strictly utility code you have the following options:

  • If you don't have to use objects for everything you could do something simple like just putting them all in a file without a class wrapper around them. This could be with or without a namespace although the namespace is recommended to prevent any issues in the future.
  • For managed C++ you can create a static class to contain them all; however, this doesn't really work the same as an actual class and my understanding is that it is a C++ anti-pattern.
  • If you aren't using managed C++ then you can just make use of static functions to allow you to access them and have them all contained in a single class. This may be useful if there are also other functions that you would want a proper instantiated object for by may also be an anti-pattern.
  • If you want to ensure that only one instance of the object containing the functions will exist then you can use the Singleton Pattern for a utility class which also allows you some flexibility in the future as you now have access to non-static attributes. This is going to be of limited use and only really applies if you need an object for some reason. Odds are if you do this you will already know why.

Note that the first option is going to be the best bet and the following three are of limited usefulness. That said though, you might encounter then just due to C# or Java programmers doing some C++ work or if you ever work on C# or Java code where the use of classes is mandatory.

share|improve this answer
Why the down-vote? –  rjzii Feb 10 '12 at 20:04
I'm not the downvoter, but probably because you are advising a class with static functions or a singleton, while free functions would probably be fine in this case (and are acceptable and useful for many things in C++). –  Anton Golov Feb 10 '12 at 21:00
@AntonGolov - Free functions are the first thing that I mentioned in the list. :) The rest of them are the more OOP oriented approaches for situations where you are dealing with "Everything must be a class!" environments. –  rjzii Feb 10 '12 at 21:09
@Rob Z: However, C++ is not one of those "Everything must be a class!" environments. –  David Thornley Feb 10 '12 at 21:12
@RobZ: I know, simply pointing out the issue many people are likely to have with your answer. –  Anton Golov Feb 10 '12 at 21:28

As you already said, copy-pasting the code is the worst form of code reuse. If you have functions that don't belong to any of your classes or may be used in for several scenarios, the best place to put them would a helper or utility class. If they don't use any instance data, they can be made static, so you don't need to create an instance of the utility class to use it.

See here for a discussion of static meber functions in native C++ and here for static classes in managed C++. You could then use this utility class wherever you would have pasted your code.

In .NET for instance, things like Min() and Max() are provided as static members on the System.Math class.

If all your functions are math-related and you would otherwiese have a gigantic Math class, you might want to break it down further and have classes like TrigonometryUtilities, EucledianGeometryUtilities and so on.

Another option would be to put shared functionality into a base class of the classes requiring said functionality. This works well, when the functions in questions need to operate on instance data, however, this approach is also less flexible if you want to avoid multiple inheritance and only stick to one base class, because you would be "using up" your one base class just to get access to some shared functionality.

share|improve this answer
IMHO, utility classes with nothing but static members are an anti-pattern in C++. You're using a class to perfectly reproduce the behaviour of a namespace, which really makes no sense. –  ipeet Feb 10 '12 at 21:46
+1 for mentioning utility classes. Languages like C# require everything to be in a class so it's quite common to create a number of utility classes for various purposes. Implementing these classes as Static make the utilities even more user friendly and avoids the headaches that inheritance can sometimes create, particularly when the base classes are becoming bloated with code that may only be used by one or two descendants. Similar techniques can be applied in other languages to provide a meaningful context for your utility functions, rather than leaving them floating in the global scope. –  S.Robins Feb 10 '12 at 21:51
@S.Robins: There's no need for anything like that in C++, you can just put them in a namespace, which has the exact same effect. –  DeadMG Feb 11 '12 at 14:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.