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
218 views
in Technique[技术] by (71.8m points)

Is there an implicit default constructor in C++?

In the book I'm reading at the moment (C++ Without Fear) it says that if you don't declare a default constructor for a class, the compiler supplies one for you, which "zeroes out each data member". I've experimented with this, and I'm not seeing any zeroing -out behaviour. I also can't find anything that mentions this on Google. Is this just an error or a quirk of a specific compiler?

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)

If you do not define a constructor, the compiler will define a default constructor for you.

Construction

The implementation of this

default constructor is:

  • default construct the base class (if the base class does not have a default constructor, this is a compilation failure)
  • default construct each member variable in the order of declaration. (If a member does not have a default constructor, this is a compilation failure).

Note:
The POD data (int,float,pointer, etc.) do not have an explicit constructor but the default action is to do nothing (in the vane of C++ philosophy; we do not want to pay for something unless we explicitly ask for it).

Copy

If no destructor/copy Constructor/Copy Assignment operator is defined the compiler builds one of those for you (so a class always has a destructor/Copy Constructor/Assignment Operator (unless you cheat and explicitly declare one but don't define it)).
The default implementation is:

Destructor:

  • If user-defined destructor is defined, execute the code provided.
  • Call the destructor of each member in reverse order of declaration
  • Call the destructor of the base class.

Copy Constructor:

  • Call the Base class Copy Constructor.
  • Call the copy constructor for each member variable in the order of declaration.

Copy Assignment Operator:

  • Call the base class assignment operator
  • Call the copy assignment operator of each member variable in the order of declaration.
  • Return a reference to this.

Note Copy Construction/Assignment operator of POD Data is just copying the data (Hence the shallow copy problem associated with RAW pointers).

Move

If no destructor/copy Constructor/Copy Assignment/Move Constructor/Move Assignment operator is defined the compiler builds the move operators for you one of those for you.
The default implementation is:

Implicitly-declared move constructor If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true:

Move Constructor:

  • Call the Base class Copy Constructor.
  • Call the move constructor for each member variable in the order of declaration.

Move Assignment Operator:

  • Call the base class assignment operator
  • Call the move assignment operator of each member variable in the order of declaration.
  • Return a reference to this.

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

...