C++11关键字

decltype

根据表达式的类型自动推导类型

int main(void)
{
    

    decltype(x) c = 21.1; //赋值
    decltype((x)) d = c; // 是一个引用
    decltype(x) e = c; //
    d = 120;
    cout << sizeof(int) << endl;
    cout << sizeof(c) << " c=" << c << endl; // c的类型按照 x的类型进行显示
    cout << sizeof(d) << " d=" << d << endl;
    cout << sizeof(e) << " e=" << e << endl;

    return 0;
}

 

 

template <typename T, typename T2>
auto ADD(T a, T2 b) -> decltype(a+b)
{
    return a + b;
}
int main(void)
{
    int a = 10;
    int b = -5;
    double x = 10.1;
    double y = -5.1;
    cout << ADD(a, x) << endl;
}

概念

C++中的链接性分为外部,内部,作用域为整个文件的变量。还有无链接性的局部变量

static

使一个变量声明为全局变量,仅在当前文件可见,外部文件不可以使用。

static放在函数中,导致局部变量存储为静态 ,当前文件函数外部不可访问。 static int a=0;

extern 

可以访问不同文件下定义的全局变量,不包括static 修饰的常量.extern a;

const

使一个变量声明为一个常量 const int a =10;具有常量的特性,不可改变其值,且链接性同static,文件外部不可访问 

说明符和限定符

register 

使变量分配到所在的寄存器

auto

自动推导类型

volatile

mutable

new和delete

定位new运算符:
double* pd1;double* pd2;
char buffer[512];
pd2 = new (buffer) double[5]; //在buffer里

普通new运算符:

pd1 = new double[5]

delete不能用与定位new运算符。

此时buffer处于静态区,delete只能释放堆区的内存

//#include "test.h"
#include <iostream>
using namespace std;

#include <new>
const int BUF = 512;
int main(void)
{

    //    int n = 5;
    char buffer[3];
    double* p;
    p = new (buffer) double[3];

//    delete [] p; //不能释放
    return 0;
}

namespace

创建自己的命名空间

//#include "test.h"
#include <iostream>
using namespace std;

#include <new>
const int BUF = 512;

namespace Jill {
int a = 10;
}

int main(void)
{

//    using namespace Jill; //编译指令
using Jill::a; //声明
    cout << Jill::a << endl; //声明
    return 0;
}

using声明和using编译的区别

using声明使特定的标示符可用,using编译指令使整个名称空间可用。
1.using声明由被限定的名称和它前面的关键字using组成,如:
using Jill::fetch;
2.using编译指令由名称空间名和它前面的关键字using nameapace组成,它使名称空间中的所有名称都可用,而不需要使用作用域解析操作符:using namespace Jill

注意:使用using编译指令导入一个名称空间中所有的名称与使用多个using声明是不一样的,而更像是大量使用作用域解析
操作符。使用using声明时,就好像声明了相应的名称一样。如果某个名称已经在函数中声明了,则不能使用using声明导入相同的名称。在上面的例子中,名称空间为全局的.如果使用using编译指令导入一个已经在函数中声明的名称,则局部名称将隐藏名称空间名,就像隐藏同名的全局变量一样。不过,仍可以像上面最后一句cin那样使用作用域解析操作符。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值