zoukankan      html  css  js  c++  java
  • C++ Primer抄书笔记(二)——变量和基本类型(下)

    四、const限定符【引用/指针/顶层/常量表达式】

    const对象值不变,必须初始化,能完成此type的大部分operation。

    一般,多文件独立变量,编译初始化仅文件内有效;

    除非,(条件:初值不是常量表达式 and 声明+定义 都有 extern);

    //file.h
    extern const int bufSize;
    //file.cc
    extern const int bufSize=fcn();

    //引用reference to const

    不可通过此reference改变object的值。

    ps:这并不代表,不可以通过其他reference来改变object的值。

    “常量引用”:引用bind对象,本身就不可能再去bind其他对象了,so引用必是const的;reference本身不是object,变不了。

    const type & 可以绑定任何表达式,甚至可以打破类型限制——但非法;

    double dVal=3.14;
    const int &ri=dVal;
    //原理
    const int temp=dVal;
    const int &ri=temp;
    //temp,即 临时量 temporary

    //指针

    pointer to const 指向常量的指针:存const type的object的address只能用它。

    ps:这并不代表,它只能存const type的object的address;即不变的是object,pointer随意。

    so:reference/pointer to const 只是它们自以为自己 to const 了。

    const pointer 常量指针:不变的是pointer,即不可以通过pointer来改变object的值。

    ps:不变的是初始化了的address,object随意。

    double dVal=3.14;
    const double cd=3.141592;
    const double *pcd=&dVal;
    const double *const pcdc=&cd;

    从右向左,pcdc是const pointer point to const double。

    //顶层const

    top-level const:必是object,任意type适用。

    low-level const:引用和pointer to const。

    const int ci=42;
    const int *pci=&ci;//low
    const int &r=ci;//low
    const int *const pci_=pci;//第一个const是low,第二个const是top

    ps:low-level const 拷贝限制:二者都要底层const资格且可类型转换。【难搞哦】

    //常量表达式const expression

    编译时得值,且值不变。

    角度刁钻,不方便使用,提供constexpr声明语句(可定义变量和函数)->供编译器提前验证其值是否是const expression。

    constexpr的定义范围:算数类型、引用、指针(初值nullptr/0/pointer to const,仅对pointer有效即顶层);一般在函数体外,函数体内可定义有效范围超出函数体的变量【比较魔幻】

    const int *p=nullptr;   //p是 pointer to const int
    constexpr int *q=nullptr;//q是 const pointer to int

    ps:constexpr会把其object置为顶层const。

    五、处理类型【类型别名/auto/decltype】

    //类型别名type alias

    typedef double wages;
    typedef wages base,*p;

    复合类型/常量:typedef定义的是类型别名,此声明符包含类型修饰符。

    typedef char *pstring;
    const pstring *ps;
    const pstring cstr=0;
    const char *cstr=0;//和楼上不是一个意思

    C++11,别名声明alias declaration:

    using SI=Sale_item;

    //auto->让编译器替你分析(through 初始值)表达式所属类型。

    auto定义的变量必须有初始值,且同一条声明中所有变量base type必须相同,类型修饰符不算。

    auto i=0,*p=&i;
    auto sz=0,pi=3.14;//

     auto会忽略顶层const,保留底层const。

    int i=0;    
    const int ci=i,&cr=ci;    
    auto d=&i;    //d是指向int型pointer
    auto e=&ci;    //e是指向const int型pointer
    const auto f=ci;  //f是const int
    auto &h=42;    //error:非常量引用不能bind字面值
    const auto &j=42; //常量引用可以bind字面值
    auto k=ci,&l=i;    //k是int,l是int型引用
    auto &m=ci,*p=&ci;    //m是const int型ci的引用,p是指向const int型 ci 的指针
    auto &n=i,*p2=&ci;    //error:n是int型i的引用,p2是指向const int型i的指针

    设置一个类型为auto的引用时,初始值中的顶层常量属性任然保留。和往常一样,如果我们给初始值绑定一个引用,则此时的常量就不是顶层常量了。【不懂】

    //decltype->要表达式的返回类型,但不要值,使目标初始化。

    编译器分析表达式并得到它的类型,但不算值。

    若,表达式是变量,返回变量type(包括顶层const和引用):

    const int ci=0,&cj=ci;
    decltype(ci) x=0;
    decltype(cj) y=x;//此处cj就是cj,而不是ci,即y绑定x

    若,表达式不是变量,返回结果对应类型:

    int i=42,*p=&i,&r=i;
    decltype(r+0) b;//b是int型
    decltype(*p) c;//error:c是int&,必须初始化

    若,多加一对(),即decltype((xxx)),编译器会误以为其是表达式,而返回引用类型(因为赋值语句左值是变量):

    decltype((i)) d;//error:d是int&,必须初始化

    六、自定义数据结构【.../头文件/预处理器】

    struct/class什么什么的。

    ps:可为类内的data member提供一个类内初始值in-class initializer。

    //头文件

    包含那些只能被定义一次的实体。

    隐式多次包含->预处理,保证程序安全正常工作。

    //预处理器preprocessor->头文件保护符header guard

    #include,编译前执行,改变部分程序

    预处理变量:已定义/未定义,必须唯一,一般大写

    #define:设定预处理变量;

    #ifdef/#ifndef:变量已定义,为真/变量未定义,为真;

    #endif:为止;

    #ifndef SALES_DATA_H
    #define SALES_DATA_H
    #include <string>
    struct Sales_data{
        std::string bookNo;
        unsigned units_sold;
        double revenue=0.0;
    };
    #endif
  • 相关阅读:
    GL_TRIANGLE_FAN Vs GL_TRIANGLE_STRIP
    Color bleeding与caustics概念解析
    Two path ray tracing与Photon Mapping(粒子跟踪)
    右手定则判断法线方向
    正确使用atoi
    深入探讨透视投影坐标变换
    gluBuild2DMipmaps与glTexImage2D与glGenTexture()函数
    OpenGL纹理贴图流程
    int main( int argc, char ** argv)在VS2008中的配置的一个实例
    c++标准库中vector数组遍历时的用法
  • 原文地址:https://www.cnblogs.com/CowryGao/p/12363458.html
Copyright © 2011-2022 走看看