Advantages of classes with only static methods in C++

前端 未结 8 1667
谎友^
谎友^ 2020-12-01 07:28

Even though there are no static classes in C++, coming from a Java background I use to create a helper class like Util containing only static methods. Is this c

相关标签:
8条回答
  • 2020-12-01 08:02

    This may be relevant to your interests. It is an article that, uh, examines the different approaches to classes and functions in Java compared to other languages.

    http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

    0 讨论(0)
  • 2020-12-01 08:11

    There is one other situation in which a static class might be preferred to a namespace: when you want to make the data private so that it can't be directly modified from outside the class/namespace, but for performance reasons you want to have have public inline functions that operate on it. I don't think there is any way to do that with a namespace, other than by declaring a class inside the namespace.

    0 讨论(0)
  • 2020-12-01 08:19

    If you want to create a collection of utility functions without clobbering the global namespace, you should just create regular functions in their own namespace:

    namespace utility {
        int helper1();
        void helper2();
    };
    

    You probably don't want to make them static functions either. Within the context of a non-member function (as opposed to a member function), the static keyword in C and C++ simply limits the scope of the function to the current source file (that is, it sort of makes the function private to the current file). It's usually only used to implement internal helper functions used by library code written in C, so that the resulting helper functions don't have symbols that are exposed to other programs. This is important for preventing clashes between names, since C doesn't have namespaces.

    0 讨论(0)
  • 2020-12-01 08:21

    In C++, classes with only static methods is mostly used in template metaprogramming.

    For example, I want to calculate fibonacci numbers at compile-time itself, and at runtime I want them to print only, then I can write this program:

    #include <iostream>
    
    template<int N>
    struct Fibonacci 
    {
       static const int value = Fibonacci<N-1>::value + Fibonacci<N-2>::value;
       static void print()
       {
           Fibonacci<N-1>::print();
           std::cout << value << std::endl;
       }
    };
    
    
    template<>
    struct Fibonacci<0>
    {
       static const int value = 0;
       static void print()
       {
           std::cout << value << std::endl;
       }
    };
    
    template<>
    struct Fibonacci<1>
    {
       static const int value = 1;
       static void print()
       {
           Fibonacci<0>::print();
           std::cout << value << std::endl; 
       }
    };
    
    int main() {
            Fibonacci<20>::print(); //print first 20 finonacci numbers
            return 0;
    }
    

    Online demo : http://www.ideone.com/oH79u

    0 讨论(0)
  • 2020-12-01 08:22

    There is no real issue with declaring static methods within a class. Although namespaces are more suitable for this purpose for the reasons mentioned in the post you are referring to.

    Using C functions can generate name collisions, unless you decide on a naming convention, prefixing your functions with stuff, for example btFunctionA, btFunctionB etc. You will want to keep your symbols within namespaces to avoid that, you are using C++ and not C after all.

    Static functions within a namespace aren't any different from non-static. I believe the static keyword is simply ignored in this context.

    0 讨论(0)
  • 2020-12-01 08:24

    C++ is a multi paradigm language, so if you need some util functions that perhaps don't really fit in a class at all, then I would just make them free functions. I don't see a reason to put them into a class, just for OOP's sake.

    There is no advantage that I can see to making all functions static and putting them in a class, over having them just as free functions. Personally, I think free functions are then an easier to work with option.

    0 讨论(0)
提交回复
热议问题