19.8.1.1. Exercises: static_cast and const_cast

  1. In Example 19.11 try moving the

    const int N = 22; 

    above or below

    int main() { 

    Observe and explain the difference in output.

    1. Predict the output of Example 19.12.

      Example 19.12. src/casts/constcast2.cpp

      #include <iostream>
      
      void f2(int& n) {
          ++n;
      }
      
      void f1(const int& n, int m) {
          if (n < m) 
              f2(const_cast<int&>(n));
      }
      
      int main() {
          using namespace std;
          int num1(10), num2(20);
          f1(num1, num2);
          cout << num1 << endl;
      }
      

      <include src="src/casts/constcast2.cpp" href="src/casts/constcast2.cpp" id="constcast2" mode="cpp"/>


    2. Remove the const_cast from the call to f2() inside f1(), and predict the output again.

[ fromfile: types.xml id: None ]