Saturday, May 9, 2009

Confused about CONST keyword in C++ and Reference variable?

Consider following code:


void main()


{


int i=10;


int %26amp;ref1=i;


int %26amp; const ref2=i;


const int %26amp;ref3=i;


}





here...


What is difference between ref1 and ref3?





because





in code we can do:





ref1++;


ref2++;


but we can not do ref3++;It gives compile time error:Const obj can't be modified.





If any body know exact and only difference between





int %26amp;ref1=i;


and


int %26amp; const ref2=i;





Both ref1 and ref2 work as normal reference to integer variable.

Confused about CONST keyword in C++ and Reference variable?
ref3 is a const reference, which means you can't modify it. You are declaring the reference itself to be const.





ref2 is a regular reference. the const keyword in this case means what the reference is bound to (in this case the variable i) will not change, which is ALWAYS the case with references. the const keyword here is totally meaningless. syntactically it is correct which is why the compiler doesn't complain. but for all intents and purposes, ref1 and ref2 are exactly the same.
Reply:%26amp; means "by reference," which is different than "by value."





When you call a variable "by reference," you are asking for its location in memory. Therefore, you can change the value of the variable simply by calling it.





For example:








int byValue(test) {


test = 6;


return test;


}





int byReference(%26amp;test) {


test = 6;


return test;


}





int test = 4; // test = 4





test2 = byValue(test); // test = 6, test2 = 6





test2 = byReference(test); // test = 6, test2 = 6





So, it matters where we put the ampersand -- it needs to be right up against the variable we want to call by reference.





In the line int %26amp;ref1 = 1, we're setting the value of ref1 to be 1, and we're doing so by reference. For all practical purposes, it's the same as saying int ref1 = 1; since we're declaring the variable, it's new to memory; since it doesn't have a reference until we create it, asking to assign its value by reference is moot.





int %26amp; const ref2 = 1 throws an error because it's basically trying to create the same constant twice. Since it errors, ref2 does not become a constant but does get assigned a value of 1.





The reason ref3 can't be changed is that even though we are calling it by reference, we have declared it a constant, and that has priority.
Reply:CONST : Constant. These are basically values that will never change. Example of this would be





CONST INT PI = 3.14159





The value of PI is always going to be that so you don't want to change that.





REFERENCE VARIABLE: Reference variables are mainly used when you are passing variables from one function to another. You pass it by reference meaning you are passing the address of the variable location. This will ensure that whatever changes you make the variable will be made to the variable.





In your program you are doing





ref2++ which will give you the error since a CONST object is something that cannot have a changing value.





You need a variable for this instead.


No comments:

Post a Comment