内存初始化 [英] Memory initialization
问题描述
我需要初始化一个包含许多变量的类(成员),
分别使用init执行它的最佳方法是什么每个成员。
es:
班级考试{
int a1;
..
...
int a100
char v1 [100]
...
ichar v100 [100]
}
Marco
Hi,
I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.
es:
class test{
int a1;
..
...
int a100
char v1[100]
...
ichar v100[100]
}
Marco
推荐答案
mcassiani写道:
mcassiani wrote:
我需要初始化一个包含很多变量的类(成员),
什么是最好的方法是每个成员单独使用init.
es:
类测试{
int a1;
..
。 。
int a100
char v1 [100]
...
ichar v100 [100]
Marco
Hi,
I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.
es:
class test{
int a1;
..
..
int a100
char v1[100]
...
ichar v100[100]
}
Marco
虽然这可能很危险你可以尝试使用memset或bzero
函数使用sizeof(类测试)作为参数...
我相信这不是一个非常对象的方法...
a1 = a100 = v1 = v100 = 0; //应该也可以...
Although this might be dangerous you might try using memset or bzero
functions using the sizeof(class test) as a paramters...
I belive this is not a very ''object'' oriented approach...
a1 = a100 = v1 = v100 = 0; // should work as well...
" JustSomeGuy" <否*** @ ucalgary.ca>在消息中写道
news:40 *************** @ ucalgary.ca ...
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:40***************@ucalgary.ca...
我需要初始化一个包含许多变量的类(成员),
I need to zero init a class with many variables (members),
虽然这可能很危险,但您可以尝试使用memof或bzero
函数使用sizeof(类测试)作为参与者......
我相信这不是一个非常对象的方法......
Although this might be dangerous you might try using memset or bzero
functions using the sizeof(class test) as a paramters...
I belive this is not a very ''object'' oriented approach...
如果这个类是POD结构,那么似乎在这种情况下,memset()
也是我的解决方案。另一方面,如果它真的是一个类 -
也许有一个vtable - 复制一个零值的静态实例工作
更好(读:memset()不工作,如果有vtable)。
问候,
Martin M. Pedersen
If the class is a POD structure, as it seems to be in this case, memset()
would be my solution too. On the other hand, if it is really a class -
perhaps with a vtable - copying a static instance with zero values works
better (read: memset() does not work if there is a vtable).
Regards,
Martin M. Pedersen
mcassiani写道:
mcassiani wrote:
我需要初始化一个包含许多变量(成员)的类,
每个单独使用init的最佳方法是什么成员。
es:
课堂测试{
int a1;
..
...
int a100
char v1 [100]
...
ichar v100 [100]
Marco
Hi,
I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.
es:
class test{
int a1;
..
...
int a100
char v1[100]
...
ichar v100[100]
}
Marco
如果会员是公开的:
班级考试
{
public:
int a1,a2;
char v1 [100],v2 [100];
};
int main()
{
测试什么= {0,0,{0},{0}};
}
>
同样适用于结构。
对于私人会员,您必须使用构造函数。
祝你好运,
Ioannis Vranos
If members are public:
class test
{
public:
int a1, a2;
char v1[100], v2[100];
};
int main()
{
test whatever={0,0, {0}, {0}};
}
The same apply for a struct.
For private members you will have to use a constructor.
Best regards,
Ioannis Vranos
这篇关于内存初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!