I can't find the answer anywhere. It can be done by using condition and throwing exception but is there any other way to do so?
-
8The question is very vague. If you don't want to construct a class object, don't do it. – David Schwartz Dec 02 '12 at 10:19
-
Hint: What's the purpose of constructors anyway? – Mark Garcia Dec 02 '12 at 10:19
-
2Please give an example of why you would want to do this to help us understand what you really want. – Vaughn Cato Dec 02 '12 at 10:24
4 Answers
It isn't clear why you would want a class that cannot be constructed under any circumstances, but you could make all constructors private
and provide no implementation for them. This will prevent construction at compile time.
In C++11 you can use delete
for all the constructors:
class A
{
public: // or private, doesn't matter.
A()=delete;
A(const A&)=delete;
A(A&&)=delete;
};

- 223,364
- 34
- 402
- 480
-
1
-
1Note that aggregate initialization messes up this whole discussion. See, e.g., [this question](https://stackoverflow.com/questions/33988297/deleted-default-constructor-objects-can-still-be-created-sometimes). – sasquires Oct 19 '18 at 17:11
Well, this is a bit of an awful hack but quite frankly any C++ application which needs a way to define an unconstructable object is probably already an awful hack!
class la
{
public:
virtual void oh_no_you_dont() = 0;
};
int main()
{
la an_instance;
return 0;
}
And under GCC, I get the following:
test.cpp: In function ‘int main()’:
test.cpp:9: error: cannot declare variable ‘an_instance’ to be of abstract type ‘la’
test.cpp:2: note: because the following virtual functions are pure within ‘la’:
test.cpp:4: note: virtual void la::oh_no_you_dont()
You can make all constructors private. This way it's impossible to create an instance of the class. You can then supply one or more static factory methods for creating the class, and by this make the users use only factory methods.

- 1,737
- 17
- 31

- 13,158
- 4
- 28
- 35
-
*"This way it's impossible to use `new` for the class."*. What if I'm not using `new`? – Mark Garcia Dec 02 '12 at 10:23
-
1`new` is irrelevant here, since the question is tagged C++ and this language lets you instantiate objects without `new`. – juanchopanza Dec 02 '12 at 10:24
Do you want your class to prevent from being constructed?
Here is a solution with an old C++ standard e.g. C++03 and older
class A {
private:
A();
A(const A&)
A(A&&);
~A()
};
Here is a solution using the latest C++ standard e.g. C++11, C++14, C++17, C++20
class A
{
A()=delete;
A(const A&)=delete;
A(A&&)=delete;
};
or even
class A final {
~A() = delete;
}
final
means that a class cannot be inherited from.
delete
for a destructor means that you can not destruct a class
Prohibiting a destructor blocks you from constructing an object also.
Why do we want to prohibit objects from being constructed/destructed?
A very common use case is that a developer wants to create a "static" class in C++.
"static" class in C++ means a class with only static methods.
This pattern is also know as an "util" class.
Instead of defining a class many people advice to use a canonical namespace
that contains static
functions for this purpose.

- 1,616
- 1
- 18
- 18