One C ++ syntax question (structural method)

In the following code, what does the following mean: next (next_par), info (info_par)?

struct list_node : public memory::SqlAlloc { list_node *next; void *info; list_node(void *info_par,list_node *next_par) :next(next_par),info(info_par) /* what the meaning of :next(next_par),info(info_par)?*/ {} list_node() { info= 0; next= this; } }; 

it looks like he wants to assign info_par for the info for info (info_par) and assign next_par for the next for the next (next_par). But I did not see the definition of next (), info ().

+4
source share
4 answers

:next(next_par),info(info_par) is an initialization list for member variables. Instead of:

 list_node(void *info_par,list_node *next_par){ next = next_par; info = info_par; } 

EDIT: as a note, initialization lists can be important if you have member variables that need to be built using a constructor other than the standard one.

+3
source

This is a C ++ initialization list. It appears only in designers. It is used to assign values ​​to the data elements of the object that is being created. The fact is that after starting the constructor, all data members have already received known values.

+1
source

This is a list of member initializers where you can initialize your members by direct calls to one of your constructors. In this case, the following and information items are initialized with parameters sent to the list_node constructor.

+1
source

This is an initialization list that initializes class or structure variables in a list. These variables are initialized before the actual array of the constructor itself is executed. This is usually not necessary (although this is a good idea, since it creates a known state for any member variables before the constructor body is executed), but it is also necessary if you are trying to initialize a base class or structure without a default constructor from a derived class or structures, since the base class must be constructed before the derived class.

For instance:

 struct base { int a; base(int b): a(b) {} }; struct derived: public base { //we have to value-initialize the base-structure before the //derived structure construtor-body is executed derived(int c): base(c) {} }; 
+1
source

All Articles