2

I was wanting to implement a simple static class that calculates the pow value of an integer in c++ while practicing. So my codes here:

#pragma once
#ifndef MATH_H
#define MATH_H
static class Math
{
    public:
        static int pow(int,int);
};


#endif /* MATH_H */

And the implemetation of pow function:

#include "Math.h"

int Math::pow(int base, int exp){
    if(exp==1)
        return base;
    else if(exp%2==0)
        return pow(base,exp/2)*pow(base,exp/2);
    else
        return pow(base,exp/2)*pow(base,exp/2)*base;
}

But the cygwin compiler is throwing compilation error:

In file included from Math.cpp:16:0:
Math.h:16:1: error: a storage class can only be specified for objects and functions
 static class Math
 ^~~~~~

Please help me solve my problem.Here are the Microsoft course's official documents from where I have been taught that there are static classes in c++

5
  • 4
    C++ doesn't have static classes. But it looks like all you need is a function. Commented Oct 15, 2019 at 18:12
  • 4
    And perhaps you want that function in a namespace Commented Oct 15, 2019 at 18:12
  • 1
    As an aside, <cmath> already has a pow function. Commented Oct 15, 2019 at 18:13
  • 1
    Remove the static before class Math. Commented Oct 15, 2019 at 18:18
  • 1
    BTW, there is a std::pow function. If you want to use your function, prefix with Math::, e.g. Math::pow. Commented Oct 15, 2019 at 18:23

1 Answer 1

7

C++ does not have "static classes" as they formally exist in other languages. You can, however, delete the default constructor (C++ >= 11) or make it private and unimplemented (C++ < 11) which has the same effect of making the type not constructible:

// C++ >= 11
class Math
{
public:
    Math() = delete;
};

// C++ < 11
class Math
{
private:
    Math(); // leave unimplemented
};

With this code, the line Math m; will fail:

(C++ >= 11) error: use of deleted function 'Math::Math()'

(C++ < 11) error: 'Math::Math()' is private within this context

However, "static classes" are mostly an anti-pattern in C++ and free functions should be preferred. (There are some cases where static classes can be useful, particularly when doing template metaprogramming. If you're not doing that then you almost certainly don't need a static class.)

Consider instead declaring the functions in a namespace:

namespace math {
    int pow(int, int);
}
Sign up to request clarification or add additional context in comments.

12 Comments

But Microsoft is presenting a course on the Edx platform and they are writing such static class containing c++ codes in Visual studio. :(
@SaswataMishra Are you sure that they aren't using the C++/CLI language? Do you see ref class anywhere?
Still haven't seen ref class. But yea, they are using c++.
@SaswataMishra It's entirely possible that the example code you were shown is incorrect. It's also possible that Microsoft's C++ compiler accepts non-standard things that no other compiler does... it wouldn't be the first time. Regardless, static class is invalid C++ according to the standard. Whether Microsoft's compiler accepts it is their problem.
I also seriously question the quality of any C++ course that even teaches about classes that aren't constructable. This is not something you should use in C++ unless you have a very good reason which is almost always template metaprogramming. Using an unconstructable class as a container for functions is wrong in C++. Languages like C# have this feature because they doesn't support free functions at all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.