1

I'm currently using a class which members I want to be the same across the whole program. So I need to disable the use of constructor of this class. What is the best way of doing that ?


  1. Declare the constructor private
  2. constructor() = delete;
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
AeroStun
  • 23
  • 1
  • 5
  • 6
    Use a namespace not a class. From the question I understand that you are only using the class name for scoping, that is best done with a namespace. – Richard Critten Sep 16 '17 at 13:51
  • Yeah, this kind of question is usually a sign that you shouldn't use a class. Unlike some other languages, not everything in C++ has to be a class, and often it shouldn't be. – underscore_d Sep 16 '17 at 13:51
  • Possible duplicate of [why explicitly delete the constructor?](https://stackoverflow.com/questions/13654927/why-explicitly-delete-the-constructor) – dtell Sep 16 '17 at 13:51
  • Singleton pattern is a thing, so that's kind-of OK. Make [the object non-copyable](https://stackoverflow.com/questions/39888012/making-a-class-non-copyable-private-undefined-methods-vs-deleted-methods) and provide a public Get method for the actual static object. My Singleton also makes the constructor private and deletes it. – Robinson Sep 16 '17 at 13:54
  • 2
    The reason why I don't use a namespace is that I still want the member variables to be private, to force the use of the member functions to manage them @RichardCritten – AeroStun Sep 16 '17 at 14:01
  • 4
    Option 2 wasn't available in C++98 so we had to use option 1. Now we don't have to. – Bo Persson Sep 16 '17 at 14:06
  • 1
    @AeroStun Using global variables is usually a mistake (but sometimes unavoidable). Perhaps the solution here is to make your "class" into an actual class and make the methods and private variables non-static. Explicitly passing around the object is extra work but worth it for the clarity it brings. – Arthur Tacca Sep 16 '17 at 14:17
  • 1
    @AeroStun If it's header only, ok. But if you use a cpp file, you can put the variables in an anonymous namespace – Rakete1111 Sep 16 '17 at 14:18
  • I would use option `2` because it gives a better error message unless you must be compatible with pre-`C++11` in which case you have to use option `1`. – Galik Sep 16 '17 at 14:22
  • 4
    Avoid the singleton pattern... write a normal class, and if you need to use a global. There's very very few cases that justify a singleton pattern, even fewer cases than justify a global. – Nir Friedman Sep 16 '17 at 14:27
  • The answer to your question is in the Effective Modern C++ book, by Scott Meyers, Item 11: Prefer deleted functions to private undefined ones, p.74. I could give you my answer too from my perspective, but it's best you got the full picture from that chapter. – Constantinos Glynos Sep 16 '17 at 14:27
  • Using static classes is bad design in 99.9% cases. Use dependency injection instead (which is just a fancy word to mean passing references around). It is a much-much clearer and flexible design. – geza Sep 16 '17 at 14:30
  • 1
    @Robinson I don't think you should put your deleted functions under the private section in your class, because C++ checks for accessibility over deleted status, and some compilers would complain without checking whether that function is deleted. – Constantinos Glynos Sep 16 '17 at 14:37

1 Answers1

5

Write the code that best explains what you're doing. If you want the class to be privately constructible, make the constructor private. If you want nobody to be able to construct the class, then delete the constructor.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982