What's the point of deleting default class constructor?

后端 未结 4 2181
轻奢々
轻奢々 2021-02-01 01:26

I\'m preparing for my CPP exam and one of the question is: Can you delete default class constructor and if so, what would be the reason to do so? OK, so obviously you can do it:

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 01:31

    Sometimes classes aren't intended to be instantiated.

    One example that hasn't been mentioned so far are 'trait' classes. For example, consider std::char_traits, although the standard hasn't said that its default constructor needs to be deleted, its default constructor is of little use because the class itself is empty, all its functions are static and it doesn't need to be instantiated to use the type aliases it provides.

    Trait classes are usually only ever used to support other classes (usually other template classes), hence it can often make sense to delete their default constructor - to reinforce the notion that they're just a helper type and not really supposed to be used like an object.

提交回复
热议问题