如何在C ++中使用构造函数(只是构造函数)初始化类中的大型私有数组? [英] How to initialize a large private array in a class using constructor(just a constructor) in c++?

查看:74
本文介绍了如何在C ++中使用构造函数(只是构造函数)初始化类中的大型私有数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个类似以下的类:

Suppose we have a class like the following one:

class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}

如您所见,我们可以使用myprogram()构造函数初始化私有成员的aa,bb,cc。

As you can see we can initialize our private members' aa, bb, cc using the myprogram() constructor.

现在,假设我有一个大型私有数组G_ [2000]。如何使用构造函数初始化该数组的所有值等于0。

Now, suppose I have a large private array G_[2000]. how I could initialize all the values of this array equal to 0 using a constructor.

class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;
double G_[2000];};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}


推荐答案

您可以编写:

    myprogram::myprogram()
    {
          for(int i=0;i<2000;i++)
             G_[i]=0;
    }

这篇关于如何在C ++中使用构造函数(只是构造函数)初始化类中的大型私有数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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