C++ structures are slightly different from C structures: C++ structures extend C structures and can have member functions. Therefore, a C++ structure is more like a class, except that all its members are public by default, which is exactly the opposite of a class. Line 5 in the code is the constructor, followed by ":" is the parameter list of the constructor, which is used to initialize member variables.
This is C++, the fifth line is the constructor of the class, which is initialized using the initialization list. Struct and class in C++ are similar (find the specific differences by yourself), but functions cannot be defined in struct in C language.
This is C++, not C.
C++ structures are slightly different from C structures: C++ structures extend C structures and can have member functions. Therefore, a C++ structure is more like a class, except that all its members are public by default, which is exactly the opposite of a class.
Line 5 in the code is the constructor, followed by ":" is the parameter list of the constructor, which is used to initialize member variables.
Isn’t this the linked list node given on LeetCode? The fifth line is the constructor.
It’s obvious if you understand struct as class
The fifth line is the constructor in C++
This is C++, the fifth line is the constructor of the class, which is initialized using the initialization list. Struct and class in C++ are similar (find the specific differences by yourself), but functions cannot be defined in struct in C language.
This is C++. Class definitions in C++ can be
struct
orclass
. The difference is thatstruct
members default topublic
and class defaults toprivate
.is the constructor of C++, and
val(x), next(NULL)
represents initialization.