vim c语言结构体成员高亮,c、cpp中使用匿名结构体、类定义数组

今天在翻unix网络编程的时候,无意中看到了使用匿名定义结构体/类定义数组的一段代码。

于是写了测试代码如下:

#include

#include

#include

#include

using namespace std;

struct st

{

int a;

int b;

char *p;

int c;

}sts[]={

{1,1,"hh",1},

{2,2,"ff",2}

};

class CObj

{

public:

int a;

string s;

int b;

}objs[]={

{1,"x",11},

{2,"y",22}

};

int main(int argc, const char *argv[])

{

for (int i = 0; i < 2; i++) {

printf("%d,%d,%s,%d\n",(sts+i)->a,(sts+i)->b,(sts+i)->p,(sts+i)->c);

}

for (int i = 0; i < 2; i++) {

printf("%d,%s,%d\n",(objs+i)->a,(objs+i)->s.c_str(),(objs+i)->b);

}

}

运行结果如下:

1,1,hh,1

2,2,ff,2

1,x,11

2,y,22

整个调用过程中,都没有用st或者CObj显示创建任何参数,调用起来要简洁很多,简直比python还要简洁。

用python的代码来表示的话:

sts = (

(1,1,"hh",1),

(2,2,"ff",2)

)

但python不支持匿名类的定义,只能用下标来访问数据,这样看来倒是C的表现更好一些呢。

记录一下,有机会在项目中用一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值