193030

01. const

Jul 8th, 2021 (edited)
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     const int i = 9;
  8.     const_cast<int&>(i) = 6; // changing cost value
  9.  
  10.     int j;
  11.     static_cast<const int&>(j); // casting static to const
  12.    
  13.     const int* p1 = &i;
  14.     int const* p4 = &i;
  15.    
  16.     cout << p1 << endl;
  17.     *p1++;
  18.  
  19.  
  20.     // cout << p1 << endl;
  21.     // 1. When const is on the left of *, data is const
  22.     // 2. If const is on the right of *, pointer is const
  23.    
  24.  
  25.     // Why use const
  26.     //      a) Guards the value
  27.     //      b) Self documenting
  28.     //      c) Enable the compiler to do more optimization, making the code tighter
  29.     //      d) Const means the variable can be put in ROM; useful for embedded
  30.  
  31.    
  32. }
RAW Paste Data Copied