找回密码
 欢迎注册
查看: 11959|回复: 14

[讨论] C/C++里的无名

[复制链接]
发表于 2011-7-8 00:47:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
无名,空,匿名,零,都是很有趣的东西。
我刚才在看书,感觉可以整理出很多东西来,
大家想到什么就说什么,最好还解释一下其存在的价值。

期望到时候能整理出一个比较完整的关于C/C++语言里 一些无名现象的全集来
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2011-7-8 00:50:25 | 显示全部楼层
我来抛个砖头:
匿名枚举:
  1. enum { value = 1 , value2 };
复制代码
  1. enum {C,L,O,V,E,R,U,S,I,T};
复制代码
摘自:http://bbs.emath.ac.cn/thread-2606-1-1.html
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2011-7-8 21:10:10 | 显示全部楼层
无名对象:
  1. #include<iostream>
  2. #include<complex>
  3. int main(){
  4. std::cout<<std::complex<double>(2,3);
  5. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2011-7-8 21:12:05 | 显示全部楼层
do{
}while(0)
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2011-7-8 21:40:20 | 显示全部楼层
也来一个,比如结构体的大小。
  1. #include <stdio.h>

  2. typedef struct{       
  3.         char  c;
  4.         int   d;
  5.         short t;
  6. }STR;

  7. int main()
  8. {
  9.         STR a;
  10.         printf("%d\n",sizeof(a));
  11.         return 0;
  12. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2011-7-8 23:28:51 | 显示全部楼层
5# G-Spider
size 为 7,
  1. #include <stdio.h>
  2. typedef struct{        
  3.         char  c;
  4.         int   d;
  5.         short t;
  6. }__attribute__((__packed__)) STR;

  7. int main()
  8. {
  9.         STR a={.c='2',.t=123};               
  10.         printf("size=%d\n%d\t%d\t%hd\n",sizeof a,a.c,a.d,a.t);
  11.         return 0;
  12. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2011-7-9 08:40:59 | 显示全部楼层
通常更加好的定义方法是:

  1. typedef struct{        
  2.       char  c;
  3.        short t;
  4.        int   d;
  5. }STR;
复制代码

点评

typedef啥意思?直接struct不行吗?  发表于 2013-10-24 09:05
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2011-11-8 23:15:55 | 显示全部楼层
struct aaa{
int cnt;
int array[0];
};
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2011-11-8 23:16:54 | 显示全部楼层
struct bbb{
int a;
char b;
int c;
};
// basic idea of offsetof
((struct bbb*)0)->c
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2011-11-8 23:17:23 | 显示全部楼层
// unnamed namespace
namespace {
void fun();
}
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-3-29 00:42 , Processed in 0.046916 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表