Constructor initialization in C ++

Source: Internet
Author: User

1. bracket assignment

Variables are defined and initialized in a program in two forms:

One is our traditional initialization form, that is, the value assignment operator,

Int A = 10;
Char B = 'R'; \ value assignment operator

Another method is to assign values in parentheses, such:
Int A (10 );
Char B ('R ');

 

The above definition and initialization form are correct and can be compiled, but the value assignment in parentheses can only be defined and initialized, and cannot be used after the definition of variables, this is different from the value assignment operator, for example:

(1)
Int A; // define a variable first
......
A = 10; // assign values as needed

(2)
Int B; // define a variable first
......
B (10); // same as (1), assign values as needed

(1) It is possible to define a variable A through compilation, but it is not initialized. When variable A is required, assign 10 to a through the value assignment operator;

(2) 10 is assigned to B through parentheses, but the compilation system considers this as a function call. The name of the function is B and 10 is the actual parameter. Therefore, the compilation is incorrect. Therefore, the value assignment in parentheses is only used to define variables and initialize them.

 

2. Colon Initialization

Now let's look at the problems of colon initialization and function initialization in constructor. Class constructor is used to call a class object to construct the data members of this class object when creating a class object, first, we need to allocate memory space for this data member. Second, we need to initialize the function data member and construct the data member in the Order stated by the data member in the class.

 

The difference between colon initialization and function body Initialization is:

The colon (:) Initialization is performed when a data member is allocated memory space. That is to say, if a data member is assigned a value expression after the colon (this expression must be a value expression in parentheses ), after the memory space is allocated, the data member is assigned a value before entering the function body, that is, the data member is initialized and the function body has not yet been executed.

Initialization in a function is performed only after all data members are allocated memory space.

 

Colon Initialization is advantageous. Some data members need to initialize the constructor before it is executed after the constructor is called, such as referencing data members, constant data members, and object data members, take a look at the following program:

 

 class student 
{public :
  student ();

  protected:
  const int a;
  int &b;
}

student ::student (int i,int j)
{
  a=i;
  b=j;
}

 

 

The student class has two data members: one is a constant data member, the other is a reference data member, and the two data members are initialized in the constructor, but this cannot be compiled, because a constant must be assigned a value during initialization, its value cannot be changed. Like a constant, a value must be assigned for initialization. After a reference is defined, it is associated with the referenced object and cannot be assigned a value. Therefore, the C ++ ":" post-initialization mechanism makes reference and constant data members possible. The student class constructor should be:
Student: Student (int I, Int J): A (I), B (j ){}

 

 class teach 
{ 
public :
teach(char *p="name",int a=0)
         
  protected:
  char name[30];
  int age;
}
teach ::teach(char*p,int a)
{
  strcopy(name ,p);
  age=a;
  cout>>name>>endl;
}
class student
{
  public:
  student (char *p="name");
         
  protected;
  char name[30];
  teach teacher;
};
student::student(char *p)
{
  strcopy(name,p);
  cont>>name>>endl;
}

 

In the above program, the compilation system will tell you that the default constructor is missing for the teacher Class Object, because the default constructor is not defined in the teach class. How can a constructor with parameters be constructed? Assign values using the colon as mentioned above. The constructor should be:

 

 student::student(char *p,char *pl,int ag):teacher(pl,ag) 
{
  strcopy(name,p);
  cont>>name>>endl;
}

 

That is to say, if a class object is a data member of another class without a default constructor, the data member should be initialized after the colon. This can contain parameters. In the class definition, for example:

Protected:
Char name [30];
Teach teacher;
Class Object cannot contain parameters because it is only declared.

Therefore, this mechanism is added in C ++, which is required for object-oriented programming. I don't know.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.