constcst.cpp

来自「希望我提供的代码对大家有帮助」· C++ 代码 · 共 27 行

CPP
27
字号
//: C24:Constcst.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Const casts



int main() {

  const int i = 0;

  int* j = (int*)&i; // Deprecated form

  j  = const_cast<int*>(&i); // Preferred

  // Can't do simultaneous additional casting:

//! long* l = const_cast<long*>(&i); // Error

  volatile int k = 0;

  int* u = const_cast<int*>(&k);

}



class X {

  int i;

// mutable int i; // A better approach

public:

  void f() const {

    // Casting away const-ness:

    (const_cast<X*>(this))->i = 1;

  }

}; ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?