Reference of Reference in C++

Passing by reference in C

Because you're passing the value of the pointer to the method and then dereferencing it to get the integer that is pointed to.

Reference of Reference in C++

It's a rvalue reference, Bjarne describes it here.

Shameless copying ("quoting"):

The rvalue reference

An rvalue reference is a compound type
very similar to C++'s traditional
reference. To better distinguish these
two types, we refer to a traditional
C++ reference as an lvalue reference.
When the term reference is used, it
refers to both kinds of reference:
lvalue reference and rvalue reference.

An lvalue reference is formed by
placing an & after some type.

A a; A& a_ref1 = a;  // an lvalue reference

An rvalue reference is formed by
placing an && after some type.

A a; A&& a_ref2 = a;  // an rvalue reference

An rvalue reference behaves just like
an lvalue reference except that it can
bind to a temporary (an rvalue),
whereas you can not bind a (non const)
lvalue reference to an rvalue.

A&  a_ref3 = A();  // Error! 
A&& a_ref4 = A(); // Ok

Meaning of pass by reference in C and C++?

In colloquial usage, "pass by reference" means that, if the callee modifies its arguments, it affects the caller, because the argument as seen by the callee refers to the value as seen by the caller.

The phrase is used independent of the actual programming language, and how it calls things (pointers, references, whatever).

In C++, call-by-reference can be done with references or pointers. In C, call-by-reference can only be achieved by passing a pointer.

"Call by value":

void foo( int x )
{
// x is a *copy* of whatever argument foo() was called with
x = 42;
}

int main()
{
int a = 0;
foo( a );
// at this point, a == 0
}

"Call by reference", C style:

void foo( int * x )
{
// x is still a *copy* of foo()'s argument, but that copy *refers* to
// the value as seen by the caller
*x = 42;
}

int main()
{
int a = 0;
foo( &a );
// at this point, a == 42
}

So, strictly speaking, there is no pass-by-reference in C. You either pass the variable by-value, or you pass a pointer to that variable by-value.

What is use of reference to reference in C++?

That isn't a reference to a reference — such a thing doesn't exist. It's an r-value reference, which is new to C++11. There are plenty of references (excuse the pun) to this concept online (e.g., here).

Does C even have pass by reference?

C parameters are always passed by value rather than by reference. However, if you think of the address of an object as being a reference to that object then you can pass that reference by value. For example:

void foo(int *x)
{
*x = 666;
}

You ask in a comment:

So why do we need pointers in C when we can pass all the parameters by value?

Because in a language that only supports pass-by-value, lack of pointers would be limiting. It would mean that you could not write a function like this:

void swap(int *a, int *b)
{
int temp = *a;
*b = *a;
*a = temp;
}

In Java for example, it is not possible to write that function because it only has pass-by-value and has no pointers.

In C++ you would write the function using references like this:

void swap(int &a, int &b)
{
int temp = a;
b = a;
a = temp;
}

And similarly in C#:

void swap(ref int a, ref int b)
{
int temp = a;
b = a;
a = temp;
}

C - pass by reference?

pass them as pointers. If you do not return the value - declare functions void, or return something

void doStuff(int *v)
{
*v = *v + 10; // main->value = 10
}

void otherStuff(int *v)
{
*v = *v - 10; // main->value = 0
}

and in the main

        doStuff(&value); // "value" = 10
/*....*/
otherStuff(&value); // "value" = 0

int *v in the function means that v is the pointer to the int object.
in the function call &value passes the pointer (address) to the value.
Dereferencing the pointer v in the function - all poerations are actuqally done on the value.

C does not support passing a variable by reference. How to do it?

You're right, C does not support passing by reference (as it is defined by C++). However, C supports passing pointers.

Fundamentally, pointers are references. Pointers are variables which store the memory address at which a variable can be located. Thus, standard pointers are comparable C++ references.

So in your case, void Foo(char *k, struct_t* &Root) would be similar to void Foo(char *k, struct_t **Root). To access the Root structure within the Foo function, you could then say something like:

void Foo(char *k, struct_t **Root){
// Retrieve a local copy of the 1st pointer level
struct_t *ptrRoot = *Root;
// Now we can access the variables like normal
// Perhaps the root structure contains an integer variable:
int intVariable = ptrRoot->SomeIntegerVariable;
int modRootVariable = doSomeCalculation(intVariable);
// Perhaps we want to reassign it then:
ptrRoot->SomeIntegerVariable = modRootVariable;
}

Thus, just passing pointers is equivalent to passing a reference.



Related Topics



Leave a reply



Submit