Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

c++ - How Many default methods does a class have?

Sorry, this might seem simple, but somebody asked me this, and I don't know for certain.

An empty C++ class comes with what functions?

Constructor, Copy Constructor, Assignment, Destructor?

Is that it? Or are there more?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In C++03 there are 4:

  • Default constructor: Declared only if no user-defined constructor is declared. Defined when used

  • Copy constructor - declared only if the user hasn't declared one. Defined if used

  • Copy-assignment operator same as above

  • Destructor same as above

In C++11 there are two more:

  • Move constructor
  • Move-assignment operator

It is also possible that the compiler won't be able to generate some of them. For example, if the class contains, for example, a reference (or anything else that cannot be copy-assigned), then the compiler won't be able to generate a copy-assignment operator for you. For more information read this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...