Home > Backend Development > C++ > C++ syntax error: member variables must be initialized in the constructor initialization list, how to deal with it?

C++ syntax error: member variables must be initialized in the constructor initialization list, how to deal with it?

王林
Release: 2023-08-21 22:46:45
Original
1320 people have browsed it

When programming in C, sometimes we encounter such a compilation error: "Member variables must be initialized in the constructor initialization list." This error usually occurs when member variables need to be initialized in the constructor of a class.

So, how should we deal with such errors? This article will introduce the member variable initialization in C in detail to help readers better understand the principles and methods of member variable initialization.

  1. Overview

In C, member variables in a class can be initialized in the following two ways:

(1) In the constructor Initialization in the initialization list

(2) Initialization in the function body of the constructor

Among them, the first method is the recommended approach. It is not only efficient, but also ensures that the member variables are in the constructor It has been properly initialized before starting execution. The second method is more troublesome, because it requires initialization of each member variable, and will also lead to multiple calls to the constructor.

  1. Constructor initialization list

The constructor initialization list refers to the part that initializes the member variables of the class in the constructor. The initial value of the member variable can be specified through the initialization list, thus avoiding the trouble of assigning values ​​in the function body.

When implementing a class, if we need to initialize the member variables of the class, we should initialize them in the initialization list of the constructor.

For example:

class Student {

private:

string name;

int age;
Copy after login

public:

Student(string name_, int age_): name(name_), age(age_){

}
Copy after login

};

at In this example, the constructor's initialization list initializes two member variables name and age in the class. Doing so can not only simplify the code, but also ensure the correct initialization of member variables.

  1. The order of member variable initialization

When using the constructor initialization list to initialize member variables, you must pay attention to the order of member variable initialization. The initialization order of class member variables in C is related to the order in which they are declared in the class.

For example:

class A {

private:

int a;

int b;
Copy after login

public:

A(int _a, int _b): a(_a), b(_b){

}
Copy after login

};

at In this example, the initialization order of member variables a and b is the same as the order in which they are declared in the class. If the order of a and b is reversed in the constructor initialization list, then a compilation error will occur because the a variable is not initialized.

  1. Summary

In C programming, the initialization of member variables is very important. In order to ensure the correctness and efficiency of the program, we should always use the constructor initialization list to initialize the member variables in the class to avoid assigning values ​​within the function body. Since the initialization order of member variables is related to the order in which they are declared in the class, you also need to pay attention to the initialization order.

The above is the detailed content of C++ syntax error: member variables must be initialized in the constructor initialization list, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template