1

I have been reading a code and I am confused about a line in the code: Here is part of the code:

class mom_set
{
public:
    int nm;
    int *mom_ind,*mode_off,*mode_count,**mode;
    int n_mom,n_eff;
    int order;
.......
.....
    mom_set(int nm0=9):nm(nm0)
    { mom_ind=new int[(nm*2+1)*(nm*2+1)*(nm*2+1)];
      mode_off=new int[3*nm*nm+1];
      mode=new int*[3*nm*nm+1];
      mode_count=new int[3*nm*nm+1];
      clear();}
......
.....
};

I am not sure how to interpret this line "mom_set(int nm0=9):nm(nm0)" . Could you please explain?

user3389597
  • 451
  • 2
  • 10
  • Is this: ["What is this weird colon-member (“ : ”) syntax in the constructor?"](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) what you're asking about ? – WhozCraig Jul 29 '15 at 15:07
  • Yes, that was another question I had about the " : " syntax. I haven't found something written this way in the C++ book I am learning from. – user3389597 Jul 29 '15 at 15:18
  • Dunno which book that is, but that doesn't sound right. Member initialization is an important part of C++, and in-fact mandatory for `const` or reference members. Perhaps look deeper in your book or [try one of these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – WhozCraig Jul 29 '15 at 15:38

1 Answers1

3

mom_set: Same name as class name means it is constructor

(int nm0=9): argument list. One argument of type int which is optional. If not passed, this argument is defaulted to value 9

:: Start of constructor initializer list

nm(nm0): Member nm is initialized with value nm0

{...}: Rest of the constructor body

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100