PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 15, 2022

[FIXED] what is this syntax mean is c++ "class_name: class_ptr_1(nullptr), class_ptr_2(nullptr) {}"

 December 15, 2022     c++, class, construct, initialization, syntax     No comments   

Issue

I am not able to understand the syntax of

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

Solution

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


Answered By - Vlad from Moscow
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing