在c++类内部使用的默认值

Default value used inside a C++ class

本文关键字:默认值 内部 c++      更新时间:2023-10-16

我一直在阅读代码,我对代码中的一行感到困惑:下面是部分代码:

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();}
......
.....
};

我不确定如何解释这行"mom_set(int nm0=9):nm(nm0)". 你能解释一下吗?

mom_set:与类名相同意味着它是构造函数

(int nm0=9):参数列表。一个可选的int类型的参数。如果没有传递,这个参数默认值为9

::构造函数初始化列表起始

nm(nm0):成员nm被初始化为值nm0

{…}:构造函数主体的剩余部分