在类中初始化表 [英] initializing table in class

查看:66
本文介绍了在类中初始化表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




请看下面的代码


/ ************** ***** /

class ctab

{

private:

static const unsigned n = 48;


public:

int tab [n];

// int tab [n] = {0}; //错误,但课外很好


ctab();

};

/ ****** ************* /


我想将标签的每个元素初始化为零。


它可以在构造函数中完成(在循环中或通过memset),但完美的

将是如果我能够声明和初始化表格,如

注释行。


所以两个问题:

1.为什么在课堂上这样做是不可能的,这是什么原因?

2.什么是解决我问题的最佳方法?在实际代码中,我有几个

数组,每个数组都有几个维度,因此循环会减慢程序速度,并且
扩展代码。 memset是唯一的选择吗?


提前致谢,

-

jimmij

Hi,

Please look at the code bellow

/*******************/
class ctab
{
private:
static const unsigned n=48;

public:
int tab[n];
//int tab[n]={0}; //error, but outside class is good

ctab();
};
/*******************/

I would like to initialize each element of tab to zero.

It can be done in constructor (in a loop or by memset), but the perfect
would be if I will be able to declare and initialize table like in
commented line.

So two questions:
1.Why it is imposible to do that in class, what is a reason for that?
2.What is a best way to solve my problem? In real code I have several
arrays, each has several dimensions, so loops will slow down program and
extend code. Is memset the only choice?

Thanks in advance,
--
jimmij

推荐答案



jimmij写道:

jimmij wrote:


请看下面的代码
/ ******************* /
class ctab
{
私人:
static const unsigned n = 48;

public:
int tab [n];
// int tab [n] = {0}; //错误,但课外很好

ctab();
};
2.什么是解决问题的最佳方法?在实际代码中,我有几个
数组,每个都有几个维度,因此循环会减慢程序并扩展代码。 memset是唯一的选择吗?
Hi,

Please look at the code bellow

/*******************/
class ctab
{
private:
static const unsigned n=48;

public:
int tab[n];
//int tab[n]={0}; //error, but outside class is good

ctab();
};
2.What is a best way to solve my problem? In real code I have several
arrays, each has several dimensions, so loops will slow down program and
extend code. Is memset the only choice?




如下所示实现ctab():

ctab :: ctab():tab( 0){/ * code * /}



Implement ctab() as so:

ctab::ctab() : tab(0) { /* code */ }


jimmij写道:
请看下面的代码

/ ******************* /
class ctab
{
私人:
static const unsigned n = 48;

public:
int tab [n];
// int tab [n] = {0}; //错误,但课外很好

ctab();
};
/ ****************** * /

我想将tab的每个元素初始化为零。

它可以在构造函数中完成(在循环中或通过memset),但是完美的注释行中声明和初始化表格,那将是。


你不能。语言不允许这样。

所以两个问题:
1.为什么在课堂上这样做是不可能的,这是什么原因?


因为非静态成员的初始化是

构造函数的任务。它是在

构造函数的初始化列表中完成的,除了数组之外的所有成员。对于数组,你需要在

构造函数体内完成。

2.什么是解决问题的最佳方法?


使用memset。

在实际代码中我有几个
数组,每个都有几个维度,所以循环会减慢程序和扩展代码。 memset是唯一的选择吗?
Please look at the code bellow

/*******************/
class ctab
{
private:
static const unsigned n=48;

public:
int tab[n];
//int tab[n]={0}; //error, but outside class is good

ctab();
};
/*******************/

I would like to initialize each element of tab to zero.

It can be done in constructor (in a loop or by memset), but the perfect
would be if I will be able to declare and initialize table like in
commented line.
You cannot. The language does not allow that.
So two questions:
1.Why it is imposible to do that in class, what is a reason for that?
Because initialisation of non-static members is the task for the
constructor. It''s done in the initialiser list of the constructor for
all members except arrays. For arrays you need to do it inside the
constructor''s body.
2.What is a best way to solve my problem?
Use memset.
In real code I have several
arrays, each has several dimensions, so loops will slow down program and
extend code. Is memset the only choice?




您还可以拥有一个包含所有这些数组的文件的静态实例

初始化(为零),并通过从

静态实例复制它们来构造对象。在封面下,编译器仍然可能会对memcpy(或memset)进行

a调用。


V

-

请在邮寄回复时从我的地址中删除资金



You could also have a static instance of your file with all those arrays
initialised (to zero), and construct your objects by copying them from the
static instance. Under the covers the compiler will still probably make
a call to memcpy (or memset).

V
--
Please remove capital As from my address when replying by mail


ro ********** @ gmail.com skrev:
jimmij写道:


请看下面的代码

/ ******************* /
类ctab {
私人:
static const unsigned n = 48;

public:
int tab [n];
// int tab [n] = {0}; //错误,但课外很好

ctab();
};
2.什么是解决问题的最佳方法?在实际代码中,我有几个
数组,每个都有几个维度,因此循环会减慢程序并扩展代码。 memset是唯一的选择吗?
Hi,

Please look at the code bellow

/*******************/
class ctab
{
private:
static const unsigned n=48;

public:
int tab[n];
//int tab[n]={0}; //error, but outside class is good

ctab();
};
2.What is a best way to solve my problem? In real code I have several
arrays, each has several dimensions, so loops will slow down program and
extend code. Is memset the only choice?



实现ctab()如下:

ctab :: ctab():tab(0){/ * code * / }



Implement ctab() as so:

ctab::ctab() : tab(0) { /* code */ }




A类{

int a [40];

A():a(0) {}

};


Comeau C / C ++ 4。3。3(2003年8月6日15:13:37)ONLINE_EVALUATION_BETA1

版权所有1988-2003 Comeau Computing。保留所有权利。

模式:严格错误C ++


" ComeauTest.c",第3行:错误:仅"()允许作为初始化程序

阵列成员

A :: a

A():a(0){}

^

在ComeauTest.c的编译中检测到
1错误。


-

TB @ SWEDEN



class A {
int a[40];
A() : a(0) {}
};

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 3: error: only "()" is allowed as initializer for
array member
"A::a"
A() : a(0) {}
^

1 error detected in the compilation of "ComeauTest.c".

--
TB @ SWEDEN


这篇关于在类中初始化表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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