-2

I am trying to use the float& operator in C++. I have just started with the language so I wanted to know the difference between float and float& operator in C++. For example when I write,

float var1;
float& var2=var1;
cout<<&var1<<endl<<&var2;

Then the output is the same address for both the var1 and var2. But if this is so, then var1 and var2 are the same variables, i.e. I'm accusing the same chunk of memory using two different reference names. Then what exactly is the difference between var1 and var2? are they the same? And also to make a carbon copy of the variable do we use float&?

2 Answers2

0

difference is more understandable in context of function. float - pass by value float& - pass by reference (does not assign new memory, points to same value as original)

Nandu
  • 808
  • 7
  • 10
0

float var1; float& var2=var1; means to make a float variable with two names, var1 and var2.

It is exactly the same as float var2; float &var1 = var2; except for the result of decltype

To make a copy you would write: float var3 = var1;

M.M
  • 138,810
  • 21
  • 208
  • 365