请教《C++ Primer》一书中有关显式类型转换的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fetich
V2EX    C++

请教《C++ Primer》一书中有关显式类型转换的问题

  •  
  •   fetich 2019-07-04 17:32:12 +08:00 3746 次点击
    这是一个创建于 2296 天前的主题,其中的信息可能已经有所发展或是发生改变。

    C++ Primer p.164

    const char* cp; // error: static_cast can ’ t cast away const char* q = static_cast<char*>(cp); static_cast<string>(cp); // ok: converts string literal to string const_cast<string>(cp); // error: const_castonly changes const ness 

    不懂第六行代码及其注释;
    这行代码是将指向 const char 的指针类型显式转换为 string ?
    但注释中 string literal 如何理解呢?难道是指 pointer 的地址字面量?

    恳请各位解惑,感谢!

    13 条回复    2019-07-05 15:20:25 +08:00
    hitmanx
        1
    hitmanx  
       2019-07-04 17:42:23 +08:00   1
    >> 这行代码是将指向 const char 的指针类型显式转换为 string ?

    >> 但注释中 string literal 如何理解呢?难道是指 pointer 的地址字面量?
    const char* cp = "abc"; // "abc"就是这里的 string literal
    stebest
        2
    stebest  
       2019-07-04 17:42:55 +08:00   1
    理解为 C 风格的字符串转化为 C++风格的 string 吧。 字符串字面量转 string
    ruoshan
        3
    ruoshan  
       2019-07-04 18:45:49 +08:00   1
    string 的构造函数有一个是这个:
    `basic_string( const CharT* s, const Allocator& alloc = Allocator() );`

    static_cast<std::string>(cp) 和 std::string(cp) 是一样的效果.

    你可以编译一下 `g++ -std=c++11 -g -S x.cc` 看一下 `x.s`. 他们两个编译出来的代码是一样的.
    fetich
        4
    fetich  
    OP
       2019-07-04 18:59:09 +08:00
    @hitmanx
    奇怪的是,cp 是未初始化的,而且括号里的 cp 前没有 * 号;如果这么解释,那省略的部分太多了,对读者来讲,很晦涩。
    fetich
        5
    fetich  
    OP
       2019-07-04 19:00:38 +08:00
    @ruoshan 惭愧,不明觉厉
    exonuclease
        6
    exonuclease  
       2019-07-04 22:32:56 +08:00   1
    因为 string 有一个构造函数是 std::string::basic_string(const char *_Ptr) 所以第二行转换可以成功 相当于 auto s1 = string(cp); 原因是 string 要支持从字符串字面量构造
    mxalbert1996
        7
    mxalbert1996  
       2019-07-04 23:06:45 +08:00 via Android   1
    "string literal"指“字符串字面值”,也就是你用双引号引用的内容,这里没有明确写出来而已。
    mxalbert1996
        8
    mxalbert1996  
       2019-07-04 23:08:54 +08:00 via Android   1
    @fetich 你记住 C 风格的字符串类型是(const char*)就行了,这个懂一点 C 的话还是很好理解的
    fetich
        9
    fetich  
    OP
       2019-07-04 23:35:36 +08:00
    @mxalbert1996 我标注一下,以后学习深入了,就理解了这部分了。
    qihehulian
        10
    qihehulian  
       2019-07-05 02:03:17 +08:00 via iPad   1
    static cast 等于没有用。string 当中的 operator 重载把等于重载了,static cast 就是直接用等于号,等同默认隐式转换。
    alexmy
        11
    alexmy  
       2019-07-05 10:16:14 +08:00
    有 4 年没有用,都忘得差不多了。
    wutiantong
        12
    wutiantong  
       2019-07-05 10:16:39 +08:00
    @qihehulian 不懂别瞎讲。
    twoconk
        13
    twoconk  
       2019-07-05 15:20:25 +08:00
    string test = const_cast<string>(cp)
    编译错误:在类型‘ std::string {aka std::basic_string<char>}’上使用 const_cast 无效,因为它既不是指针,也不是引用,也不是数据成员指针

    string test = static_cast<string>(cp); 等同于:string test = cp; (隐式类型转换)

    char* q = const_cast<char*>(cp);、//const_cast 转换符是用来移除变量的 const 修饰符,所以这行代码是对的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2714 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 14:55 PVG 22:55 LAX 07:55 JFK 10:55
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86