在构造函数中冒号是什么意思? [英] What does the colon mean in a constructor?

查看:389
本文介绍了在构造函数中冒号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复项

C ++奇怪的构造函数语法

构造函数中的冒号后的变量

在C ++构造函数名后面有冒号(:)是什么?

对于下面的C ++函数:

For the C++ function below:

cross(vector<int> &L_, vector<bool> &backref_, vector< vector<int> > &res_) : 

    L(L_), c(L.size(), 0), res(res_), backref(backref_) {

    run(0); 

}

冒号(:)告诉关系之间的左右部分?可能,这段代码可以说什么?

What does the colon (":") tell the relationships between its left and right part? And possibly, what can be said from this piece of code?

推荐答案

这是一种在c类之前初始化类成员字段的方法

This is a way to initialize class member fields before the c'tor of the class is actually called.

假设你有:

class A {

  private:
        B b;
  public:
        A() {
          //Using b here means that B has to have default c'tor
          //and default c'tor of B being called
       }
}

现在通过写入:

class A {

  private:
        B b;
  public:
        A( B _b): b(_b) {
          // Now copy c'tor of B is called, hence you initialize you
          // private field by copy of parameter _b
       }
}

这篇关于在构造函数中冒号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆