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 165 views
2 answers 151 views
151 views asked May 30, 2018 by avibootz
1 answer 140 views
140 views asked May 13, 2021 by avibootz
2 answers 152 views
1 answer 112 views
112 views asked Feb 23, 2022 by avibootz
1 answer 224 views
1 answer 183 views
...