answersLogoWhite

0

What is reference variable in c plus plus?

Updated: 8/10/2023
User Avatar

Upadhyayangelash92

Lvl 1
14y ago

Best Answer

A reference is not a variable of any kind. A reference is simply an alias; an alternate name for an object that already exists. It is analogous to a person with the birth name Robert who might also be known as Robbie, Rob, Rab, Bob and so on. They are all aliases for the same person, so every operation we perform on Bob or Robbie is actually performed on Robert.

Once we assign an object to a reference, we cannot subsequently assign another object to that same reference. Thus a reference is always guaranteed to refer to the same object for as long as that reference remains in scope. Moreover, a reference can never be null; it must always refer to something. As such, references must be assigned to an object at the point of declaration, just as you would a constant.

Note that although a reference is not a variable, the object it refers to can be, unless the reference itself is explicitly declared constant. By the same token, a non-constant reference cannot refer to a constant object.

References are typically used whenever we need to pass an object to a function by reference rather than the usual by value semantics. Like so:

void by_value (int x) {}

void by_reference (int& y) {}

In the above examples, x is a copy of the object we pass to the function, so any changes made to x will not affect the object we passed. However, y is a reference thus any changes made to the object referred to by y will be reflected in the object we passed.

A pointer variable is also a type of reference, but it behaves quite differently. Firstly, a pointer variable really is a variable; it is used to store a memory address thus it requires memory of its own. Secondly, unless the pointer is declared constant, we can change the address a pointer refers to by changing its value. Finally, a pointer may be null.

References are generally much easier to work with and more intuitive than pointers. For instance, we do not need to dereference a reference, thus we can use the member-of operator (.) rather than the pointer-to-member operator (->) when working with object references. Moreover, given that a reference can never be null, there is no need to test for null as we would with a pointer. When passing an object by reference, the function can simply use the reference, safe in the knowledge the reference refers to a valid object. If we pass a pointer, we lose that guarantee and must test the pointer before attempting to dereference it. Nevertheless, there can often be cases where passing an object to a function is optional, in which case we use a pointer argument defaulting to null.

There is also one other type of reference known as an r-value reference. An r-value is an operand that appears on the right-hand-side of an operator. Typically, an r-value is a constant since we would normally expect to only modify the left-hand-operand (the l-value). However, since C++11, move semantics require that an r-value be modifiable. For that reason we use r-value references.

We typically use r-value references when declaring move constructors and move assignment operators, like so:

struct S {

S (const S&); // copy constructor

S& operator= (const S&) // copy assignment

S (S&&); // move constructor

S& operator= (S&&) // move assignment

}

Note that S& is a reference while S&& is an r-value reference. The reason we need an r-value reference is that r-values that are moved are always assumed to be objects that will shortly fall from scope (such as when return an object by value). Thus move semantics must leave the r-value in an unspecified but valid state in order to allow the object to be destroyed. To achieve that, the r-value must be modifiable, hence we use an r-value reference.

The classic scenario is when moving a vector. Copying vectors is highly inefficient, but moving them is simply a case of switching ownership of the resource being moved; the array itself. Once the l-value has taken ownership of the resource, the r-value can simply be switched to an empty state. Note that we don't actually clear the vector (the resource no longer belongs to the r-value) we simply change its internal to state to reflect that of an empty vector. The vector can then be allowed to safely fall from scope.

It should be noted that the C++ compiler may well implement references as constant pointers and r-value references as pointer variables, however that is a matter of concern only to compiler designers. When programming in C++, it is vital that we understand that a pointer is a type while a reference is a programming tool.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

A reference variable is a variable that refers to another variable.

int x = 7;

int &r = x;

r would be a reference to x.

If you change the value of x the value of r will change and the other way around.

A reference will always be the same as the variable it refers to.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A reference is an alias, an alternative name for an existing variable. When you pass variables to a function by reference, you are effectively passing the variable itself. The function assigns the variable to a local reference, hence it is an alternate name for that variable. When you pass by value you create a copy of the variable.

Note that unlike pointers, references consume no memory of their own. They are not variables, they simply refer to existing variables. But since they are not variables, they can only be assigned at the point of instantiation and cannot be reassigned while they remain in scope.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A constant reference is an object for which its immutable members cannot be altered by the function that receives it. In other words, if you pass a constant reference to an integer, the integer cannot be changed by the function you pass it to -- it is constant.

You must use constant references in copy constructors. After all, you do not wish to change the immutable members of the object being passed, you only wish to copy its members. Similarly for the assignment operator (=) and comparison operators (==, !=, >, >=, <, <=).

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A constant modifier is simply the use of the keyword const . It is used to ensure variables are not altered. When used at the end of a member function, it is safe to assume the immutable member variables will not be altered when the function is called.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A reference parameter is a function parameter where the function signature indicates that the argument be passed by reference rather than by value.

Note that an argument is a variable that is passed to a function by the caller, while a parameter is the variable that is actually received by the function. The two are actually separate variables because, by default, all variables are passed by value in C/C++.

When you pass an argument to a function by value, the function makes a temporary copy of the argument. The copy is the parameter that is actually used by the function, and will fall from scope when the function returns. As such, any changes made to the parameter are not reflected back in the argument that was originally passed.

When a reference parameter is used, however, the parameter refers to the argument itself. Thus any changes made to the reference are reflected in the argument that was originally passed.

Example:

#include

void ByVal( int i )

{

printf( "ByVal: i=%d at memory address 0x%.8x\n", i, &i );

i *= 2;

printf( "ByVal: i=%d at memory address 0x%.8x\n", i, &i );

}

void ByRef( int& i )

{

printf( "ByRef: i=%d at memory address 0x%.8x\n", i, &i );

i *= 2;

printf( "ByRef: i=%d at memory address 0x%.8x\n", i, &i );

}

void ByPtr( int* i )

{

printf( "ByPtr: i=0x%.8x at memory address 0x%.8x with indirect value %d\n", i, &i, *i );

*i *= 2;

printf( "ByPtr: i=0x%.8x at memory address 0x%.8x with indirect value %d\n", i, &i, *i );

}

int main()

{

int x = 1;

printf( " Main: x=%d at memory address 0x%.8x\n", x, &x );

ByVal( x );

printf( " Main: x=%d at memory address 0x%.8x\n", x, &x );

ByRef( x );

printf( " Main: x=%d at memory address 0x%.8x\n", x, &x );

ByPtr( &x );

printf( " Main: x=%d at memory address 0x%.8x\n", x, &x );

return 0;

}

Output:

Main: x=1 at memory address 0x0021f964

ByVal: i=1 at memory address 0x0021f890

ByVal: i=2 at memory address 0x0021f890

Main: x=1 at memory address 0x0021f964

ByRef: i=1 at memory address 0x0021f964

ByRef: i=2 at memory address 0x0021f964

Main: x=2 at memory address 0x0021f964

ByPtr: i=0x0021f964 at memory address 0x0021f890 with indirect value 2

ByPtr: i=0x0021f964 at memory address 0x0021f890 with indirect value 4

Main: x=4 at memory address 0x0021f964

As you can see, both ByVal and ByRef do the same thing, doubling the parameter that was passed to it. However ByVal has no effect on the argument x because the parameter i refers to a completely different memory location that has the same value as x (just as if we'd written int i = x). We double i but we do not double x, and i falls from scope when the function returns. But in ByRef, we pass argument x by reference, therefore parameter i refers to the same memory location as argument x. Thus when we double the value of parameter i we also double the value of argument x -- because they are the same variable.

It should be noted that you cannot pass a pointer by reference, they are always passed by value (as shown in ByPtr). However, pointers act just like references anyway, the only difference being that a temporary pointer variable must be instantiated to create the function parameter. If you want to refer to the pointer itself (so that you can change the value of the pointer, rather than just the value being pointed at), you must pass a pointer-to-pointer type to the function instead, both as argument and parameter. This adds an additional level of indirection and also creates a temporary variable (because it is still passed by value), but it is the only way to effectively pass a pointer by reference.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is reference variable in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is an alias name given to a variable in opp with c plus plus?

An alias is a reference, an alternate name for a variable or constant. You can assign the address of any variable or constant to a reference of the same type. A reference is a bit like a constant pointer to the type but, unlike a pointer, a reference has no address of its own thus you cannot store references. More importantly, references can never be NULL. They are simply an alternative name by which you can refer to an existing variable or constant. When you assign a value to an existing reference to a variable, you are assigning the value to the variable itself. When you pass a reference to a function, you are passing the address of the value being referred to, and that address is assigned to the function's reference argument and is local to the function. This is not unlike passing a pointer, but pointers may be NULL, references are guaranteed to be non-NULL (a NULL reference invalidates your program). Note that C++ references are not the same as C reference variables or constants. In C, a reference variable is simply a non-const pointer, while a reference constant is a constant pointer. Hence pointers can be dereferenced (both in C and C++). But in C++, a reference is neither a variable nor a pointer, but is constant (it always refers to the same object and cannot be reassigned once assigned).


How do you use call by reference in c plus plus?

Call by reference means calling a function using a reference to a variable or a pointer. You call a function by passing refrences to a variable. For eg: void x(int &amp;a) { a=2; } void main() { int s=3; x(s); } OR void a(int &amp;c) { c=5;}void main(){ int *p; *p=2a(*p);}


How do you update a data file in c plus plus with a reference?

You cannot store references. A reference is nothing more than an alias, an alternate name for an existing variable or constant. References are primarily used when passing variables to functions such that the function can operate upon the variable itself -- known as passing by reference. The function refers to the variable by a different name, an alias, but it is the same variable. By contrast, when passing a variable by value the function uses a copy of that variable, assigning the variable's value to that copy. References are often confused with pointers, primarily because C uses the term to mean a pointer (hence the term, dereferencing). But in C++ a reference is a separate entity altogether. Unlike a reference, a pointer is a variable in its own right, one that can be used to store a memory address. Since a pointer has storage, you can store a pointer in a data file. However, in reality you are only storing the pointer's value -- a memory address -- not an actual pointer. Pointers and references are similar insofar as they can both refer to an object. A pointer does this by storing the memory address of the object, while a reference refers directly to the object itself. Thus if you have a pointer and a reference to the same object, the pointer's value is exactly the same as the address of the reference. Therefore the only way you can store a reference is by storing the object being referred to, not the reference itself.


What is a primitive type variable in c plus plus?

same the types used in C. that is int...char...float...


What is difference between direct addressing and indirect addressing in c plus plus?

Indirect addressing uses a pointer. Indirectly accessing the memory being pointed at is known as dereferencing. Direct addressing uses a variable's name or a reference to obtain the value.

Related questions

What is a reference variable in c plus plus?

A reference variable in C++ is a formal parameter of a function call that automatically dereferences itself, as if it were a pointer, into a reference to the original value in the calling routine. You declare the reference type in the function declaration and prototype, but the compiler automatically adds the reference (&amp;) operator on call, and the dereference (*) operator on use.


What is an alias name given to a variable in opp with c plus plus?

An alias is a reference, an alternate name for a variable or constant. You can assign the address of any variable or constant to a reference of the same type. A reference is a bit like a constant pointer to the type but, unlike a pointer, a reference has no address of its own thus you cannot store references. More importantly, references can never be NULL. They are simply an alternative name by which you can refer to an existing variable or constant. When you assign a value to an existing reference to a variable, you are assigning the value to the variable itself. When you pass a reference to a function, you are passing the address of the value being referred to, and that address is assigned to the function's reference argument and is local to the function. This is not unlike passing a pointer, but pointers may be NULL, references are guaranteed to be non-NULL (a NULL reference invalidates your program). Note that C++ references are not the same as C reference variables or constants. In C, a reference variable is simply a non-const pointer, while a reference constant is a constant pointer. Hence pointers can be dereferenced (both in C and C++). But in C++, a reference is neither a variable nor a pointer, but is constant (it always refers to the same object and cannot be reassigned once assigned).


How do you use call by reference in c plus plus?

Call by reference means calling a function using a reference to a variable or a pointer. You call a function by passing refrences to a variable. For eg: void x(int &amp;a) { a=2; } void main() { int s=3; x(s); } OR void a(int &amp;c) { c=5;}void main(){ int *p; *p=2a(*p);}


What does a constant do in c plus plus?

A constant is a variable that does not change. The correct term is constant variable.


When would you use a pointer and a reference variable?

pointer: to access data by address reference: there is no reference in C language


What is syntax in c plus plus for declaring a variable?

type variable {[optional array size]} {= optional initializer};


Is the word main an illegal variable name in C PLUS PLUS?

I can tell you that it is not an illegal variable name in C. I do not currently have a C++ compiler installed, but I would assume that it would also be valid in C++.


What do you mean by initialization in c plus plus?

Not initialized variable: int myInt; Initialized variable: int myInt = 10;


Most c plus plus plus programmers use uppercase letters for variable names?

No.


How do you update a data file in c plus plus with a reference?

You cannot store references. A reference is nothing more than an alias, an alternate name for an existing variable or constant. References are primarily used when passing variables to functions such that the function can operate upon the variable itself -- known as passing by reference. The function refers to the variable by a different name, an alias, but it is the same variable. By contrast, when passing a variable by value the function uses a copy of that variable, assigning the variable's value to that copy. References are often confused with pointers, primarily because C uses the term to mean a pointer (hence the term, dereferencing). But in C++ a reference is a separate entity altogether. Unlike a reference, a pointer is a variable in its own right, one that can be used to store a memory address. Since a pointer has storage, you can store a pointer in a data file. However, in reality you are only storing the pointer's value -- a memory address -- not an actual pointer. Pointers and references are similar insofar as they can both refer to an object. A pointer does this by storing the memory address of the object, while a reference refers directly to the object itself. Thus if you have a pointer and a reference to the same object, the pointer's value is exactly the same as the address of the reference. Therefore the only way you can store a reference is by storing the object being referred to, not the reference itself.


What is a primitive type variable in c plus plus?

same the types used in C. that is int...char...float...


Explain reference variable and how it is different from pointer variable?

In JAVA, all variables are reference variables, and there are no pointer variables. Even though the platform may implement them as pointers, they are not available as such. In C, no variables are reference variables. They are a C++ enhancement. In C++ a reference variable is syntactically the same as a pointer variable, except that the use of the indirection operator (*) is implicit. You do declare reference variables slightly differently than pointer variables but, once you do so, they can be treated as non-pointer variables. Reference variables also cannot be redefined once they have been initialized to point to some object. They are const. Structurally, there is no difference between a pointer variable and a reference variable. They are both still pointers. The compiler just makes it easier to treat reference variables and non-pointer variables the same way.