Igor
Igor

Reputation: 27258

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

Upvotes: 9

Views: 1089

Answers (5)

Tomek
Tomek

Reputation: 4659

The best way to prevent creation of non-heap objects is to make destructor private. Then there is no way compiler can destruct the object when it goes out of scope and it will complain. This will not prevent anyone from doing new however.

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507215

Like others said, a namespace is what you should use. If you want to stay with your class, create a class that has a private constructor, and derive from it, to make your intention obvious:

class NonConstructible { 
    NonConstructible();
};

class SuperUtils: NonConstructible {
    static void foo();
    // ...
    static std::vector<int> globalIDs;
    // ...
};

Ok, now let's look into the namespace which are the one and only way to do this:

namespace SuperUtils {
    void foo() {
        // ....
    }

    std::vector<int> globalIDs;
};

You can call that using SuperUtils::foo(); in both cases, but the namespace has the advantage that in a scope you can use the namespace declaration and directive to bring certain or all members into the current scope, so that you can reference them without using SuperUtils:::

void superFunction() {
    using namespace SuperUtils;
    foo();
}

While generally that should be avoided, it can be helpful when the method is using exclusively much stuff from SuperUtils, which then can improve the readability of the code.

Upvotes: 10

Ferruccio
Ferruccio

Reputation: 100718

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.

Upvotes: 20

Mark Ransom
Mark Ransom

Reputation: 308452

In order to use a copy constructor you have to have an object to copy, so if you've locked down the default constructor you should be safe.

Upvotes: 3

JaredPar
JaredPar

Reputation: 755269

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.

Upvotes: 11

Related Questions