-1

I am not able to understand the syntax of

class_name: class_ptr_1(nullptr), class_ptr_2(nullptr) {}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    These core C++ fundamentals should be explained in every C++ textbook, is there something specific in your textbook's explanation that's unclear? – Sam Varshavchik Jul 15 '22 at 16:26

1 Answers1

0

It seems you mean

class_name() : class_ptr_1(nullptr), class_ptr_2(nullptr) {}
          ^^^

It is a constructor definition with a mem-initializer list. That is the class data members class_ptr_1 and class_ptr_2 are initialized in the mem-initializer list.

Here is an example

#include <iostream>
#include <string>

struct Beginner
{
    Beginner() : first_name( "Deepak" ), last_name( "Singh" )
    {
    }

    std::string first_name;
    std::string last_name;
};

int main()
{
    Beginner beginner;

    std::cout << "first name: " << beginner.first_name
              << ", last name: " << beginner.last_name
              << '\n';
}

The program output is

first name: Deepak, last name: Singh
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335