V2EX const cast

Const Cast

释义 Definition

const cast(通常写作 const_cast)是 C++ 中的类型转换运算符,用来添加或移除类型的 const / volatile 限定(最常见的是“去掉 const”),以便在需要非 const 参数的接口中传递指针或引用。
注意:如果对象本来就是 const,通过 const_cast 去掉 const 后再修改它,属于未定义行为(undefined behavior)。

发音 Pronunciation (IPA)

/knst kst/

词源 Etymology

这是 C++ 术语中的“组合词/构造名”:const 来自 constant(常量、不可修改的限定),cast 来自“类型转换(casting)”的传统说法。C++ 在早期 C 风格强制转换的基础上,引入了更明确的四种命名转换(如 static_castdynamic_castconst_castreinterpret_cast),其中 const_cast 专门处理 const/volatile 限定。

例句 Examples

I used const_cast to pass a cons pointer to an old API.
我用了 const_cast 把一个 const 指针传给旧接口。

Although const_cast can remove constness from a reference, modifying an object that was originally declared const is undefined behavior in C++.
虽然 const_cast 可以从引用上移除 const 限定,但在 C++ 中修改一个最初就被声明为 const 的对象会导致未定义行为。

相关词 Related Words

文献与作品 Notable Works

  • The C++ Programming Language(Bjarne Stroustrup)中讨论 C++ 的命名转换运算符体系(包含 const_cast)。
  • C++ Primer(Lippman, Lajoie, Moo)在类型系统与指针/引用章节中介绍 const_cast 的用途与风险。
  • *Effective C++*(Scott Meyers)在 const 正确性(const-correctness)与接口设计相关条目中提及 const_cast 的谨慎使用。
  • ISO C++ Standard(C++ 国际标准文档)在“显式类型转换(explicit type conversion)”相关条款中规范 const_cast 的语义与限制。
About     Help     Advertise     Blog     API     FAQ     Solana     6212 Online   Highest 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 58ms UTC 02:18 PVG 10:18 LAX 19:18 JFK 22:18
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