本文介绍了什么是const X cx = {1};的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是什么意思

其中X是

struct X

{

int i;

}

const X cx = {1};


帮助

sandspiderX

Hi,

What is meant by
where X is
struct X
{
int i;
}
const X cx={1};

Help
sandspiderX

推荐答案




它正在用cx.i初始化cx分配给1.与X cx相同; cx.i = 1;


更多例子:


struct font

{

int size;

char * family;

int color_red;

int color_green;

int color_blue;

bool粗体;

bool斜体;

};


//你可以写

font myfont = {12," times new roman",255,10,10,true,false};


//而不是

font myfont2;

myfont2.size = 12;

myfont2.family =" times new roman" ;;

myfont2 .color_red = 255;

myfont2.color_green = 10;

myfont2.color_blue = 10;

myfont2.bold = true;

myfont2.italic = false;


问候,

ben



It''s initializing cx with cx.i assigned to 1. Same as X cx; cx.i = 1;

more example:

struct font
{
int size;
char* family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};

// you can write
font myfont = {12, "times new roman", 255, 10, 10, true, false};

// instead of
font myfont2;
myfont2.size = 12;
myfont2.family = "times new roman";
myfont2.color_red = 255;
myfont2.color_green = 10;
myfont2.color_blue = 10;
myfont2.bold = true;
myfont2.italic = false;

regards,
ben




它正在初始化cx,cx.i分配给1.与X cx相同; cx.i = 1;

更多例子:

struct font
{int / size>
char * family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};

//你可以写
字体myfont = {12," times new roman",255,10,10,true,false};

//而不是
字体myfont2;
myfont2.size = 12;
myfont2.family =" times new roman" ;;
myfont2.color_red = 255;
myfont2.color_green = 10;
myfont2.color_blue = 10;
myfont2.bold = true;
myfont2.italic = false;

问候,



It''s initializing cx with cx.i assigned to 1. Same as X cx; cx.i = 1;

more example:

struct font
{
int size;
char* family;
int color_red;
int color_green;
int color_blue;
bool bold;
bool italic;
};

// you can write
font myfont = {12, "times new roman", 255, 10, 10, true, false};

// instead of
font myfont2;
myfont2.size = 12;
myfont2.family = "times new roman";
myfont2.color_red = 255;
myfont2.color_green = 10;
myfont2.color_blue = 10;
myfont2.bold = true;
myfont2.italic = false;

regards,
ben




但是你还要添加''const''关键字,它告诉编译器

你不会改变X的值。


Allan



but you are also adding the ''const'' keyword which says to the compiler that
you are not going to change the value of X.

Allan





这是一种初始化结构变量cx的方法,使得cx.i =

1.如果在X中声明了更多成员,那么你可以做到

{1,3 ....}


const关键字告诉cx变量一旦初始化将不会

有修改其成员的价值。



This is a way to initialise the structure variable cx such that cx.i =
1. If there were more members declared in X then you could have done
{1,3....}

The const keyword tells that the cx variable once intialised will not
have the value of its members modified.


这篇关于什么是const X cx = {1};的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 17:04