Should I define a default constructor?

So we did a peer review, and this slight disagreement rose,

Should a default constructor be defined even if it does nothing, or should we let the compiler define it?

Until now, none of the parties could come up with any serious advantages or disadvantages. What are the pros and cons of each style and which one is considered “cleaner”?

+4
source share
2 answers

Most likely, it will be closed as “mostly opinion-based,” but I can give you some objective points:

  • , - , . , - , .

  • ( .cc/.cpp), , . , , .

    • - , , , .

  • . , (, , , ).

  • , =default ++ 11.

, . , , , , , , . ( , .)

+7

- c'tors

, - , , .

, = default ++ 11.

struct MyObject {
    // only members that the compiler initializes fine 
    std::vector<int> data_;
    MyObject() = default;
};

, , ( ++ 11):

struct MyObject {
    // only members that the compiler initializes fine 
    std::vector<int> data_;
};

c'tors

c'tor, - c'tor. c'tor , , - , "", : -)

c'tor

  • (-)
  • (o) , (= default)
  • (-) c'tor
  • (-), ( "", ), , default-c'tor, destructor, copy, move, -. .
  • (-) , c'tor noexcept? . , .

(+), .

+1

Source: https://habr.com/ru/post/1530112/


All Articles