Static (?) class

Hello All,

I want to create a Math class with some special functions in it. So far I have created this class and then inherited it into each class that needs to use it.

I think there must be a better way, and I seem to recall reading about it some time ago. I would prefer not to have to instantiate the class, but rather use it similarly to the "cout" function.

I can't remember if it needs to be declared as a static class, but I have been battling to find how to do it.

Basically, I would like to simply include the header file for my Math class and then use it.

Any input into how I can achieve this?

Thanks
Rob

You can do like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Math.h
class Math
{
public:
    static double sin(double);
    //Other static functions
}

//MAth.Cpp
double Math::sin(double x)
{
    //...
}

//Usage
double x =Math::sin(5);

But it is better to just create some functions in dedicated namespace:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Math.h
namespace Math
{
    double sin(double);
    //Other functions
}

//MAth.Cpp
double Math::sin(double x)
{
    //...
}

//usage
double x =Math::sin(5);

http://stackoverflow.com/questions/1434937/namespace-functions-versus-static-methods-on-a-class
Topic archived. No new replies allowed.