Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 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?

question from:https://stackoverflow.com/questions/4044275/how-many-default-methods-does-a-class-have

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

1 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...