(Note): List of constructor initialization and constructor Initialization
1. To better describe the initialization list, an error code is first introduced:
1 # include 2 3 # include "string. h "4 using namespace std; 5 6 class Student 7 {8 private:
Since constructor initialization lists and constructor body assignments can assign values to member variables, what is the difference between them?
Constructors can be performed in two phases: (1) initialization phase, (2) General calculation stage.
Java-4.7 constructor initialization (1)
In this section, let's talk about the initialization of the constructor.
(1) domain initialization before the constructor Initialization
package com.ray.testobject;public class Test {private int id;public Test(
The constructor initialization list starts with a colon, followed by a list of data members separated by commas (,). Each data member is followed by an initialization formula in parentheses. For example:
Class
Cexample
{ Public : Int A;
The constructor initialization list is sometimes necessary. Although the constructor is divided into initialization and computing stages, it is more efficient to use the initialization constructor list. This is one of them and must be used in some
The constructor initialization list begins with a colon, followed by a comma-delimited list of data members followed by an initialization in parentheses after each data member. For example:Class Cexample {Public:int A; float B; Constructor
The initialization of classes in C + + typically consists of four parts:1. Constructor initialization list2. Assigning values in the constructor body3. Class external initialization4. Assigning values directly when declaring a classFor internal data
The constructor initialization list begins with a colon, followed by a comma-delimited list of data members followed by an initialization in parentheses after each data member. For example:class? Cexample? {Public:???? int? A;???? float? b;????
The constructor initialization list begins with a colon, followed by a comma-delimited list of data members followed by an initialization in parentheses after each data member. For example:Class Cexample{Publicint A;float B;Constructor
10. C ++-constructor initialization list, object construction sequence, destructor, 10. c Constructor
First, recall,Previously learned const
Separate useConst ModificationVariables are defined.ConstantFor example, const int I = 1;
UseVolatile const
The C ++ constructor has three tasks:
1. Allocate space.
2. initialize members.
3. Execute the initialization function body.
To improve efficiency and other reasons, an initialization list is introduced. The call to the initialization list is called
First, what is the output of the running C ++ code?
class A{private:int n1;int n2;public:A(): n2(0) , n1(n2 + 2){}void Print(){cout
Answer: The output N1 is a random number, and N2 is 0. In C ++, the initialization sequence of member variables is
Case without parent class1, static code blocks--Static member variables--member variables (instance properties)--code block--constructors2, static code blocks and static member variables are initialized only onceThere are cases of parent class1,
Test{Test (test& t1){this->t=t1;}PrivateTest T;}I believe a lot of people, like me, are very confused. Why test constructs a constructor that calls test and then calls the assignment operator of the test class. That is because the assignment
In C ++, the constructor has a special initialization method called "initial expression table (initialization table)", which is located after the parameter table of the function, but before the function body, this indicates that the initialization
Java-4.7 constructor initialization (3)
Next, go to the previous chapter.
(4) initialize static explicit Fields
Some attribute fields are initialized using static code blocks.
package com.ray.testobject;public class Test {private static int
There are two ways to initialize a member variable in a C + + class:
Constructor initializes the list and the constructor body assignment. Here's how the two approaches are different.
The order in which member variables are initialized is in the
///simple use of classes and objects#includeusing namespacestd;///overloading of constructors, calculating volumeclassBox { Public: Box (); ///constructor with no arguments to initialize theBox (intHintWintL): Height (h), Width (w), Length (l) {};///
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.