1

I have a class with non static functions. Because of the architecture of my program I think it will be better to use static functions since the class is just an utility. In some cases I just need one function of the class so I see unnecessary to create the object. Basically I have this:

class StaticCall
{
public:
    StaticCall(){}
    static int call_1()
    {
        std::cout << "In call 1" << std::endl;
        call_2();
        return 0;
    }
    static int call_2();
    {
        std::cout << "In call 2" << std::endl;
        return 0;
    }
};
int main( int argv, char** argc )
{
    std::cout << "Calling 1" << std::endl;
    StaticCall::call_1();
    std::cout << std::endl;
    std::cout << "Calling 2" << std::endl;
    StaticCall::call_2();
    return 0;
}

It works fine, but I was wondering if there could be any problems with this method. I can achieve the same by using a namespace as other posts already say. But since I already have the class I want to do it with static functions.

Garf365
  • 3,619
  • 5
  • 29
  • 41
Luis
  • 127
  • 1
  • 11
  • no problems; `call_2` from `call_1` always be called rightly because they are in the same class – nikniknik2016 Mar 11 '16 at 09:17
  • no problems, the only thing to consider is that in c++ not everything has to be a class and one could argue that this class is not the most appropriate construct for this situation. e.g. one can create an instance of this class, even if it does not make sense. I would at least make the constructor private. – 463035818_is_not_an_ai Mar 11 '16 at 09:20
  • 1
    A namespace with pure functions will do fine. you don't really need any class at all. Rule-of-thumb: when there is no data member/s in a class it might be better to use pure functions. – Jojje Mar 11 '16 at 09:25
  • Related to [using-a-namespace-in-place-of-a-static-class-in-c++](http://stackoverflow.com/questions/14361408/using-a-namespace-in-place-of-a-static-class-in-c) – Jarod42 Mar 11 '16 at 09:41
  • 1
    Your last argument sounds like: I come from Java so I want to do it the Java way even in C++. – knivil Mar 11 '16 at 09:44
  • @Jojje: Not at all! If the design shows related functions it is a good idea to implement this relationship between the design idea and the code in classes or namespaces. Standalone functions are like global vars. Sometimes easy to use but in bigger software a startpoint of unmaintainability. – Klaus Mar 11 '16 at 10:06
  • @Klaus, yes but as you say namespaces will be fine, I recommend nesting your namespaces. If you add data member/s at a later point you will probably need to do some refactoring anyhow. – Jojje Mar 11 '16 at 10:14

1 Answers1

2

what you are doing here is to use a class to encapsulate two methods - the same as a namespace would do. You are essentially replacing the use of a namespace with a class.

Does it work?? Yes, it does!

Is it ok to do this? Well, from a code maintenance/readability point of view, not so much, because:

  • you can inherit from a class
  • you can instantiate a class
  • static methods don't belong to any class instance and this may cause problems later down the line, especially when expanding the class.

If all you want is encapsulation, or a way to not pollute the global namespace - that's why you have namespaces.

The code is cleaner, more readable and a lot less complicated when you properly use the language features c++ offers.

Pandrei
  • 4,843
  • 3
  • 27
  • 44