How to use references to float in C++

1 Answer

0 votes
#include <iostream>

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

int main()
{
	float f = 3.14F;
	float  &ref = f;

	cout << "f = " << f << "  ref = " << ref << endl;

	ref *= 3;
	cout << "f = " << f << "  ref = " << ref << endl;

	const float &cref = f;
	
	cout << "f = " << f << "  cref = " << cref << endl;

	//cref *= 4; // Error C3892 'cref': you cannot assign to a variable that is const	

	return 0;
}

/*
run:

f = 3.14  ref = 3.14
f = 9.42  ref = 9.42
f = 9.42  cref = 9.42

*/

 



answered May 23, 2018 by avibootz

Related questions

...