struct初始化 [英] struct initialization

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

问题描述

大家好,

我是C ++编程的新手。有些机构可以解释如何初始化

结构值,考虑这个程序,


#include< iostream.h>

struct mystruct

{

int a,b,c,d,e,f;

};

class myclass

{

mystruct x;

public:

myclass(){}

void display(){

cout<<" Hi guys";

}

};


int main()

{

myclass me;

me.display( );


}

如果你在gdb中调试这个程序并在display()中停止它并看到

的值英寸×"它们都包含一些垃圾值。我知道我们必须在构造函数中初始化struct的值,为了克服这个问题,我们必须明确地为每个值分配值


这样的结构成员,


myclass(){xa = 0; xb = 0; xc = 0; xd = 0; xe = 0; xf = 0但是,当你必须初始化

的结构成员是巨大的时候,你的代码将会变得混乱。他们是否可以一次性为结构中的所有

成员分配零值?我的意思是在Linux上用什么函数可以用来做这个初始化?


有些正文请解释一下,

Seema Rao

Hi all,
I am new to C++ programming. Can some body explain how to intialize the
structure values, consider this program,

#include <iostream.h>
struct mystruct
{
int a,b,c,d,e,f;
};

class myclass
{
mystruct x;
public:
myclass(){}
void display(){
cout <<"Hi guys ";
}
};

int main()
{
myclass me;
me.display();

}
If you debug this program in gdb and stop it in display() and see the
values of "x" they all contain some garbage values. I know we have to
intialize the values of struct in the constructor, to over come this
problem, we have to explicitly assign values to each and every
member of your structure like this,

myclass () { x.a=0;x.b=0;x.c=0;x.d=0;x.e=0;x.f=0;} But your code will
become messy when the members of the structure you have to intialize
are huge. Is their any way I can assign zero values to all of the
members of the structure at one shot? I mean any function that can be
used on Linux to do this initialization??

Some body please explain,
Seema Rao

推荐答案

seema写道:
seema wrote:

#include< iostream .h>

struct mystruct

{

int a,b,c,d,e,f;

};
#include <iostream.h>
struct mystruct
{
int a,b,c,d,e,f;
};


mystruct x;
mystruct x;


很大。他们是否可以一次性为结构中的所有

成员分配零值?我的意思是在Linux上使用的任何函数都可以用来进行初始化吗?
are huge. Is their any way I can assign zero values to all of the
members of the structure at one shot? I mean any function that can be
used on Linux to do this initialization??



memset怎么样

memset((void *)& x,0,sizeof(mystruct));

how about memset
memset((void*)&x,0,sizeof(mystruct));




seema写道:

seema wrote:

大家好,

我是C ++编程的新手。有些机构可以解释如何初始化

结构值,考虑这个程序,


#include< iostream.h>
Hi all,
I am new to C++ programming. Can some body explain how to intialize the
structure values, consider this program,

#include <iostream.h>



应该是:

#include< iostream>

should be:
#include <iostream>


struct mystruct

{

int a,b,c,d,e,f;

};
struct mystruct
{
int a,b,c,d,e,f;
};



为什么不在这里使用初始化列表定义一个ctor - 将初始的

值更改为你需要的值:


struct MyStruct

{

int a,b,c,d,e,f;

MyStruct(): a(0),b(0),c(0),d(0),e(0),f(0){}

~MyStruct(){}

};

Why not define a ctor here with an init list - change the initial
values to whatever you need:

struct MyStruct
{
int a,b,c,d,e,f;
MyStruct() : a(0), b(0), c(0), d(0), e(0), f(0) { }
~MyStruct() { }
};


>

class myclass

{

mystruct x;

public:

myclass(){}
>
class myclass
{
mystruct x;
public:
myclass(){}



myclass():MyStruct(){ }

~myclass(){}

myclass() : MyStruct() { }
~ myclass() { }


void display(){

cout<<" ;嗨伙计们;

}
void display(){
cout <<"Hi guys ";
}



无效显示()

{

std :: cout<< a = << a<< " \ n";

std :: cout<< b = << b<< " \ n";

std :: cout<< c = << c<< " \ n";

std :: cout<< d = << d<< " \ n";

std :: cout<< e = << e<< " \ n";

std :: cout<< f = << f<< std :: endl;

}

void display()
{
std::cout << "a = " << a << "\n";
std::cout << "b = " << b << "\n";
std::cout << "c = " << c << "\n";
std::cout << "d = " << d << "\n";
std::cout << "e = " << e << "\n";
std::cout << "f = " << f << std::endl;
}


};


int main()

{

myclass me;

me.display();


}

如果你在gdb中调试这个程序并在display()中停止它,并看到

值为x的值。它们都包含一些垃圾值。我知道我们必须在构造函数中初始化struct的值,为了克服这个问题,我们必须明确地为每个值分配值


这样的结构成员,


myclass(){xa = 0; xb = 0; xc = 0; xd = 0; xe = 0; xf = 0但是,当你必须初始化

的结构成员是巨大的时候,你的代码将会变得混乱。他们是否可以一次性为结构中的所有

成员分配零值?我的意思是在Linux上使用的任何函数都可以用来进行初始化吗?
};

int main()
{
myclass me;
me.display();

}
If you debug this program in gdb and stop it in display() and see the
values of "x" they all contain some garbage values. I know we have to
intialize the values of struct in the constructor, to over come this
problem, we have to explicitly assign values to each and every
member of your structure like this,

myclass () { x.a=0;x.b=0;x.c=0;x.d=0;x.e=0;x.f=0;} But your code will
become messy when the members of the structure you have to intialize
are huge. Is their any way I can assign zero values to all of the
members of the structure at one shot? I mean any function that can be
used on Linux to do this initialization??



这描述了对默认构造函数的需求。你可以在上面的例子中添加参数化的ctors。你不是限制

只提供一个ctor。


This describes the need for default constructors. You could just as
well add parametized ctors in the examples above. You are not limited
to only provide a single ctor.


>

有些人请解释一下,

Seema Rao
>
Some body please explain,
Seema Rao


他们无法在mystruct中声明构造函数,因为

i提到

这个结构只是为了说明。但在实际情况下,我使用的结构来自

库,它是theora_info结构。

Salt_Peter写道:
No their is no way i can declare a constructor in the mystruct because
i mentioned
this structure just for illustration. But in real case the structure I
am using is from the
library, it''s theora_info structure.
Salt_Peter wrote:

seema写道:
seema wrote:

大家好,

我是C ++编程的新手。有些机构可以解释如何初始化

结构值,考虑这个程序,


#include< iostream.h>
Hi all,
I am new to C++ programming. Can some body explain how to intialize the
structure values, consider this program,

#include <iostream.h>



应该是:

#include< iostream>


should be:
#include <iostream>


struct mystruct

{

int a,b,c,d,e,f;

};
struct mystruct
{
int a,b,c,d,e,f;
};



为什么不在这里使用初始化列表定义一个ctor - 将最初的

值更改为你需要的值:


struct MyStruct

{

int a,b,c,d,e,f;

MyStruct(): a(0),b(0),c(0),d(0),e(0),f(0){}

~MyStruct(){}

};


Why not define a ctor here with an init list - change the initial
values to whatever you need:

struct MyStruct
{
int a,b,c,d,e,f;
MyStruct() : a(0), b(0), c(0), d(0), e(0), f(0) { }
~MyStruct() { }
};



class myclass

{

mystruct x;

public:

myclass(){}

class myclass
{
mystruct x;
public:
myclass(){}



myclass():MyStruct(){}

~myclass(){}


myclass() : MyStruct() { }
~ myclass() { }


void display(){

cout<<" Hi guys" ;;

}
void display(){
cout <<"Hi guys ";
}



无效显示()

{

std :: cout << a = << a<< " \ n";

std :: cout<< b = << b<< " \ n";

std :: cout<< c = << c<< " \ n";

std :: cout<< d = << d<< " \ n";

std :: cout<< e = << e<< " \ n";

std :: cout<< f = << f<< std :: endl;

}


void display()
{
std::cout << "a = " << a << "\n";
std::cout << "b = " << b << "\n";
std::cout << "c = " << c << "\n";
std::cout << "d = " << d << "\n";
std::cout << "e = " << e << "\n";
std::cout << "f = " << f << std::endl;
}


};


int main()

{

myclass me;

me.display();


}

如果你在gdb中调试这个程序并在display()中停止它,并看到

值为x的值。它们都包含一些垃圾值。我知道我们必须在构造函数中初始化struct的值,为了克服这个问题,我们必须明确地为每个值分配值


这样的结构成员,


myclass(){xa = 0; xb = 0; xc = 0; xd = 0; xe = 0; xf = 0但是,当你必须初始化

的结构成员是巨大的时候,你的代码将会变得混乱。他们是否可以一次性为结构中的所有

成员分配零值?我的意思是在Linux上使用的任何函数都可以用来进行初始化吗?
};

int main()
{
myclass me;
me.display();

}
If you debug this program in gdb and stop it in display() and see the
values of "x" they all contain some garbage values. I know we have to
intialize the values of struct in the constructor, to over come this
problem, we have to explicitly assign values to each and every
member of your structure like this,

myclass () { x.a=0;x.b=0;x.c=0;x.d=0;x.e=0;x.f=0;} But your code will
become messy when the members of the structure you have to intialize
are huge. Is their any way I can assign zero values to all of the
members of the structure at one shot? I mean any function that can be
used on Linux to do this initialization??




这描述了对默认构造函数的需求。你可以在上面的例子中添加参数化的ctors。你不是限制

只提供一个ctor。



This describes the need for default constructors. You could just as
well add parametized ctors in the examples above. You are not limited
to only provide a single ctor.



有些人请说明,

Seema Rao

Some body please explain,
Seema Rao


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

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