C/C++常用trick整理

来源:互联网 发布:邮箱域名是什么意思 编辑:程序博客网 时间:2024/06/07 22:15

以下涉及到的内容,均为网上搜集,并非本人原创

1.构造函数互相调用

class A
{
public:
    A()
    
{
        
new (this)A( 5 );
    }

    A( 
int n  ):_x(n)
    
{
    }

    
int _x;
}
;

2.编译期判断结构大小是否合法

struct xxx
{
    
int u;
}
;
const int SIZE= 4;
typedef 
char xxx_check_size[sizeof(struct xxx) == SIZE? 1 : -1];

在程序中加入如上的代码,即可在编译接管判断结构xxx的大小是否为4字节,如果不是,则会报数组下标为负的错误

 

 
原创粉丝点击