C++11/C++14 (五)STATIC ASSERTIONS AND CONSTRUCTOR DELEGATION

PS:以下代码在VS2015编译通过~~~

1. Static assertions 在程序编译期间进行断言判断

The static_assert declaration tests a software assertion at compile time. This can be especially useful for template code.

syntax looks like this:

static_assert{bool_constexpr, string}

举例如下:

       //run-time assert
char* ptr = "hello";
assert(ptr != NULL);


// compile-time assert
static_assert(sizeof(void*) == 4, "64-bit is not supported");

         编译错误 1 error C2338: 64-bit is not supported


2.Constructor delegation

c++03中,一个类的构造函数是不能调用其他类的构造函数,必须自己构造类成员或调用成员方法。

请注意,在c++ 03中,类的非常量数据成员不能在这些成员声明的站点上初始化。 它们只能在构造函数中初始化。 在c++ 11中,现在是允许的.

举例如下:
//c++03 
class A
{
void init(){ cout << "init()" << endl; }
void dosomething(){ cout << "dosomething()" << endl; }
public:
A(){ init(); }
A(int a){ init(); dosomething(); }
};


//c++11
class B
{
int b = 99;
void dosomething(){ cout << "dosomething()" << endl; }
public:
B(){}
B(int b) :B(){ dosomething(); }
};

Copy constructor using constructor delegation
A(const A& b) : A()
{
  m_a = b.m_a;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值