结构体中的成员的访问属性, 能不能设置为只读的?

Ethan_yushui213 2009-01-04 04:06:16
比如:
struct _A
{
int index;
char name[260];
};

我希望index的值是只读的,name的值是可写的
能不能做到呢?

劳请高手赐教。
...全文
521 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzdwfwt111 2009-01-04
  • 打赏
  • 举报
回复
const int index;
「已注销」 2009-01-04
  • 打赏
  • 举报
回复
const
fhinalibob 2009-01-04
  • 打赏
  • 举报
回复
struct _A
{
const int index;
char name[260];
};
yellowhwb 2009-01-04
  • 打赏
  • 举报
回复
VC6中如果定义这样的struct:
typedef struct
{
const int ik;
int jk;
} TS;
如果在代码中使用:
TS po;
是会编译错误的,“error C2512: 'TS' : no appropriate default constructor available”,估计是编译器把这个struct当class来处理了!
lbh2001 2009-01-04
  • 打赏
  • 举报
回复

#include <stdio.h>

typedef struct _A
{
const int index;
char name[260];
}AA;

int main(void)
{
AA a = { 120, "adfadf"}; /*index的值只能在初始化时给出*/

printf("%d\n", a.index);

return 0;
}
waizqfor 2009-01-04
  • 打赏
  • 举报
回复
const 就OK了
const int index
数组正常写就可以了
lbh2001 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 xiaoyisnail 的回复:]
给个例子吧:

C/C++ code
#include <stdio.h>

struct _A
{
const int index;
char name[260];
};

int main()
{
struct _A a = {12, "abcd"};
printf("%s\n", a.name);
printf("%d\n",a.index);
//a.index = 18;//这里如果不注释掉,编译会报错,F:\>gcc 1.c
//1.c: In function 'main':
//1.c:14: error: assignment of read-only memb…
[/Quote]

嗯,index的值只能在初始化时给出
fvan 2009-01-04
  • 打赏
  • 举报
回复
C++ struct 可以使用const
C92 const 不能用在 struct(ANIC)
C99 可以用 const (VC的cl不符合C99,编译不了)
xiaoyisnail 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 xiaoyisnail 的回复:]
给个例子吧:
C/C++ code#include<stdio.h>struct_A
{constintindex;charname[260];
};intmain()
{struct_A a={12,"abcd"};
printf("%s\n", a.name);
printf("%d\n",a.index);//a.index = 18;//这里如果不注释掉,编译会报错,F:\>gcc 1.c//1.c: In function 'main'://1.c:14: error: assignment of read-only member 'index'return0;
}
[/Quote]

补充:纯c环境
xiaoyisnail 2009-01-04
  • 打赏
  • 举报
回复
给个例子吧:

#include <stdio.h>

struct _A
{
const int index;
char name[260];
};

int main()
{
struct _A a = {12, "abcd"};
printf("%s\n", a.name);
printf("%d\n",a.index);
//a.index = 18;//这里如果不注释掉,编译会报错,F:\>gcc 1.c
//1.c: In function 'main':
//1.c:14: error: assignment of read-only member 'index'

return 0;
}
yellowhwb 2009-01-04
  • 打赏
  • 举报
回复
const需要在class中用,struct我刚试了,不行!
canican 2009-01-04
  • 打赏
  • 举报
回复
const int在vc6 中通过了
lbh2001 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lbh2001 的回复:]
引用 1 楼 jieao111 的回复:
const

不可能的,在结构中成员加const在纯C中是非法的
[/Quote]
不好意思,记错了,应该是static是非法的
但有个问题,index的初始值怎么办
yellowhwb 2009-01-04
  • 打赏
  • 举报
回复
需要在class中用,struct不行
chenzhp 2009-01-04
  • 打赏
  • 举报
回复
const int
xiaoyisnail 2009-01-04
  • 打赏
  • 举报
回复
c99开始有const了

struct _A
{
const int index;
char name[260];
};
yellowhwb 2009-01-04
  • 打赏
  • 举报
回复
const来修饰就可以了
const int index;
lbh2001 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jieao111 的回复:]
const
[/Quote]
不可能的,在结构中成员加const在纯C中是非法的
yellowhwb 2009-01-04
  • 打赏
  • 举报
回复
const来修饰就可以了
const char ch1;
wyswyg63 2009-01-04
  • 打赏
  • 举报
回复
用const
const int index;
加载更多回复(1)

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧