Defaulted constructor vs implicit constructor

后端 未结 2 1163
走了就别回头了
走了就别回头了 2021-01-19 23:59

It\'s possible that someone has already asked about this but googling for \"default\", \"defaulted\", \"explicit\" and so on doesn\'t give good results. But anyway.

2条回答
  •  春和景丽
    2021-01-20 00:04

    Given below excerpt from C++ Programming Language Stroustrup 4th Edition book makes it clear that they are equivalent.


    Explicit Defaults

    Since the generation of otherwise default operations can be suppressed, there has to be a way of getting back a default. Also, some people prefer to see a complete list of operations in the program text even if that complete list is not needed. For example, we can write:

    class gslice {
        valarray size;
        valarray stride;
        valarray d1;
    public:
        gslice() = default;    //<< Explicit default constructor
        ~gslice() = default;
        gslice(const gslice&) = default;
        gslice(gslice&&) = default;
        gslice& operator=(const gslice&) = default;
        gslice& operator=(gslice&&) = default;
        // ...
    };
    

    This fragment of the implementation of std::gslice (§40.5.6) is equivalent to:

    class gslice {
        valarray siz e;
        valarray stride;
        valarray d1;
    public:
        // ...
    };
    

提交回复
热议问题