Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent creation of class whose member functions are all static

All the member variables and member functions in my class ClassA are static.

If a user is trying (by mistake) to create an object of this class, he receives a warning: "ClassA, local variable never referenced", because all the functions are static, so this object is never referenced. So, I want to prevent the user from trying to create an object of this class.

Would it be enough to create a private default (no variables) constructor? Or do I have to also create private copy constructor and private assignment operator (to prevent using the default constructors)? And if I do have to create them too, maybe it would be better just to create some dummy pure virtual function instead, and this will prevent the user from creating an object?

Thank you

like image 994
Igor Avatar asked Dec 09 '08 17:12

Igor


2 Answers

Instead of using a class with all static methods, you may be better off making the methods free-standing functions in a separate namespace. The call syntax would be the same:

namespace::function() instead of classname::function()

and you don't need to deal with someone trying to instantiate your class.

like image 143
Ferruccio Avatar answered Sep 20 '22 20:09

Ferruccio


Creating a private default constructor should be sufficient. Both of the other default constructs (copy constructor and assignment) rely on having an instance to work correctly. If there is no default constructor then there is no way to create an instance, hence no way to actually get to the copy construction part.

It would likely save you a few headaches though to define all 3 as private and not implemented.

like image 43
JaredPar Avatar answered Sep 19 '22 20:09

JaredPar