I have two misconceptions about the C + + default constructor: If a class does not define any constructors, the compiler (definitely!) will define a composite default constructor for the class. The composite default constructor initializes all the
C ++ Object Model -- Construction of Default Constructor (chapter 2)Chapter 2 The most common complaint about C ++ In The constructors of Constructor is that The compiler has done too many things on The back of programmers. the Conversion operator
Construction operations of default constructor in the C ++ object model, and object model constructorA class. If there is no user-declared constructor, a default constructor is implicitly declared. The implicitly declared constructor determines when
A class, if there is no constructor for the user declaration, then a default constructor is implicitly declared. The implicit declaration of the constructor, exactly when to be synthesized, by the compiler composition of the default constructor
InArticleBefore getting started, I first pointed out two common misunderstandings about C ++ beginners:
1. If no default constructor is defined for any class, it will be merged.
II. The default constructor synthesized by the compiler will
C + + terms-everything about the default constructorDefault constructor (Defaultconstructor):The default constructor is the constructor that is used to create the object when no display initial value is provided. If no constructors are provided, C +
We often say that when life is a class, if we do not specify a constructor for the class, then the compiler will implement a connstructor for us, then this is a certain right?In fact, this is wrong. This is not what I said, is to understand the C + +
Misunderstanding of default constructors1. When the program ape defines the default constructor, the compiler will directly use this default constructor.A simple exampleClass Student;Class School{Public:School (){}...Student students;};We know that
C ++ default constructor
The default constructor is described in three parts of C ++ primer:
P44 variable initialization rules
P227 functions (constructor)
Class 8 (constructor initialization)
P392 default constructor
I. Variable initialization
Question: for C + + beginners There is an error, if the class does not define any constructors, the compiler will automatically generate the default constructor.
NOTE: This statement is wrong.
The correct argument is that the compiler will not
When I had nothing to do during the holidays, I downloaded a master's VC ++ video materials. When talking about C ++, it is said that if the programmer does not define the default constructor, the compiler will automatically generate a default
1. Examples of programs with errors Class Foo{public:int M_nval; Foo *m_pnext;}; void Foo_bar (void) {Foo Bar; if (Bar.m_nval | | bar.m_pnext) {int i; i++; }} (1) The correct program semantics is to require Foo to have a default constructor,
There are 4 situations that cause the compiler to synthesize the default constructor for classes that are not declared constructor.1. Member class object with default constructorIf a class does not have any constructor, but contains a member object
Source program in c ++:
Copy codeThe Code is as follows: class X {
Private:
Int I;
};
Int main (){X x;}
The preceding class X does not define the constructor and only has an int I.
The assembler is as follows:
Copy codeCode:; 7: int main (){
Push
source programs in C + +:
Copy Code code as follows:
Class X {
Private
int i;
};
int main () {x x;}
The class X above does not have a constructor defined, just an int i.
The following is an assembler:
Copy Code
In C ++, if the programmer has no explicit definitionDefault constructor)The compiler will generate one, which is implicitly declared, as needed.
There are two types of Default constructors for implicit Declaration: trivial (useless) constructor,
1. Default Initialization
If no initial value is specified during variable definition, the variable is initialized by default, and the variable is assigned the "default value ".
For class variables, initialization relies on constructors. Therefore,
Turn from: Click to open the link
The default constructor refers to a constructor that provides a default value for all parameters, usually a parameterless constructor. For example, the following class test, whose default constructor is test
class controls the default initialization process through a special constructor, which is the default constructor, and the default constructor does not require any arguments. The definition default constructor we can display also allows the compiler
The role of the default constructor1. Default constructorsA C + + default constructor is a constructor that provides a default value for a parameter in a class, typically an empty function with no parameter value, or a constructor that provides
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.