How to use const_cast on a const reference in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

void square_root(const int &n) {

	const_cast<int &> (n) = n * n;
}
int main()
{
	int n = 9;

	square_root(n);

	cout << n << endl;

	return 0;
}


/*
run:

81

*/

 



answered May 30, 2018 by avibootz

Related questions

1 answer 170 views
2 answers 159 views
159 views asked May 30, 2018 by avibootz
1 answer 143 views
143 views asked May 13, 2021 by avibootz
2 answers 158 views
1 answer 114 views
114 views asked Feb 23, 2022 by avibootz
1 answer 227 views
1 answer 189 views
...