Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of classes with only static methods in C++

Tags:

c++

static

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 considered bad style or usual practice? One alternative I see is to use C functions (no class context at all). What other alternatives are there? What are there advantages and disadvantages and under which circumstances would I use any of these.

defining bunch of static methods in c++ suggests namespacing static functions as one alternative, though I fail to see what effects the static keyword without class context has.

like image 258
dcn Avatar asked Sep 08 '11 09:09

dcn


People also ask

What is the advantage of using static methods?

A static method belongs to the class rather than the object of a class. A static method can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.

Can a class have only static methods?

What is Utility Class? Utility Class, also known as Helper class, is a class, which contains just static methods, it is stateless and cannot be instantiated. It contains a bunch of related methods, so they can be reused across the application.

Why do we need static methods in class?

Static methods have a limited use case because, like class methods or any other methods within a class, they cannot access the properties of the class itself. However, when you need a utility function that doesn't access any properties of a class but makes sense that it belongs to the class, we use static functions.

Are static classes better?

Static classes and static members are useful because they do not require instances created for each new object. That means, they consume fewer resources and no duplication of the same class or member is needed in memory. Static members make code cleaner.


2 Answers

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.

like image 79
James O'Doherty Avatar answered Sep 21 '22 19:09

James O'Doherty


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

like image 35
Nawaz Avatar answered Sep 24 '22 19:09

Nawaz