PracticeDev/study_cpp/cpp_primer_source_code/Chapter 15/constcast.cpp

29 lines
536 B
C++
Raw Normal View History

2022-12-20 17:31:11 +08:00
// constcast.cpp -- using const_cast<>
#include <iostream>
using std::cout;
using std::endl;
void change(const int * pt, int n);
int main()
{
int pop1 = 38383;
const int pop2 = 2000;
cout << "pop1, pop2: " << pop1 << ", " << pop2 << endl;
change(&pop1, -103);
change(&pop2, -103);
cout << "pop1, pop2: " << pop1 << ", " << pop2 << endl;
// std::cin.get();
return 0;
}
void change(const int * pt, int n)
{
int * pc;
pc = const_cast<int *>(pt);
*pc += n;
}