Reputation: 13997
I have a static class in C++, i.e. all members of this class are static.
Since I do not want the class to be instantiated ever, I make the constructor private. I did not define a default copy assignment operator or copy constructor for this class, since this can never be instantiated. However, my static code analyzer tells me:
If you have declared a destructor because your class manages resources, you may want to declare a copy constructor and copy assignment operator too for the same reason.
-> Is it OK if I do not define a destructor for this class? Since it can never be created, it cannot be destroyed either, I suppose?
-> Is this a good practice? Anything wrong with my design?
Upvotes: 2
Views: 1633
Reputation: 726559
Since I do not want the class to be instantiated ever, I make the constructor private.
If you have access to a C++11 compiler, you have a better option: you can explicitly delete the "gratuitous" constructor provided by the compiler. This should address the problem with the destructor, because the compiler would know that it is impossible to construct your class.
If you cannot use the deleted constructor feature, declaring a constructor private should be sufficient to prevent outside instantiations. The destructor is not necessary, because there is nothing to destruct.
Is this a good practice? Anything wrong with my design?
This practice is more common in languages where it is not possible to define free-standing functions/variables. C++, on the other hand, provides free-standing functions and variables. A combination of these two features with namespaces makes classes with only static members unnecessary: such class would be functionally equivalent to a namespace, but would not be idiomatic to C++.
Upvotes: 6