What's the point of deleting default class constructor?

后端 未结 4 2275
轻奢々
轻奢々 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

    Multiple default choices

    Deleting the default constructor of a class is a good idea when there are multiple choices for the default or uninitialised state. For example, suppose I have a class,

    template
    class Polynomial;
    

    which represents a polynomial over a field, F. In this case, there are many choices for a default value of the polynomial. One could be the identity for polynomials under addition, i.e. zero, but we could also have the multiplicative identity, unity. It depends how the user intends to reason about the class and how it is used.


    No default choice

    The other notable case is when instead of having multiple default states that could make sense, we have none. For example, suppose we have a class representing a manifold or surface.

    What then is Manifold()? It's an empty space, with nothing, no surface, no notion of distance or metric. But then it doesn't make sense to think of it as a manifold, but rather perhaps something more general like a topological space.

    So, in this case, I would opt to delete the default constructor as well.

提交回复
热议问题