Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

References in C++

Tags:

c++

I have just started C++ and have come across references and have not understood completely.

References , as i read is an alternative name for an object.Why use that instead of directly accessing the object as any operation on references is directly reflected on the object ...?

  1. Why and when are they used ?
  2. Is ist like a constant pointer that is referenced each time it is used ... ?

And , it says

 double& dr = 1;  ----  says it is an error (some lavalue needed) 
 const double& cdr = 1;  ---- says it is ok. 

i dont understand it properly..So please explain why it is so ...

Thank You...:)

like image 572
Flash Avatar asked Aug 17 '10 13:08

Flash


People also ask

Are there references in C?

No, it doesn't. It has pointers, but they're not quite the same thing. For more details about the differences between pointers and references, see this SO question.

What is meant by reference in C?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

How do references work in C?

The main use of references is acting as function formal parameters to support pass-by-reference. In an reference variable is passed into a function, the function works on the original copy (instead of a clone copy in pass-by-value). Changes inside the function are reflected outside the function.

What is the reference operator in C?

The reference operator (@expression) lets you refer to functions and variables indirectly, by name. It uses the value of its operand to refer to variable, the fields in a record, function, method, property or child window.


2 Answers

Why use that instead of directly accessing the object as any operation on references is directly reflected on the object ...?

C++ passes parameters by value, meaning if you have a function such as:

void foo(MyObject o) { ... }

By default C++ will make a copy of a MyObject, not directly use the object being passed in. So, one use of references is to ensure you are working on the same object:

void foo(MyObject &o) { ...}

Or, if you aren't modifying o:

void foo(const MyObject &o) { ... }
like image 99
Justin Ardini Avatar answered Oct 04 '22 06:10

Justin Ardini


References are another way of what was originally in C code like this

void fubarSquare(int *x){
  int y = *x;
  *x = y * y;
}

// typical invocation
int z = 2;
fubarSquare(&z);
// now z is 4

with references in C++ it would be like this

void fubarSquareCpp(int& x){
   x = x * x;
}

// typical invocation
int z = 2;
fubarSquareCpp(z);
// now z is 4

It's a neater syntactical way of using a call-by-reference parameter instead of using the C's notation asterisk/star to indicate a pointer and as a call-by-reference parameter...and modifying the parameter directly outside of the function...

Have a look at Bjarne Stoustrap's page here which covers how C++ is and also here on the technical faq here

like image 22
t0mm13b Avatar answered Oct 04 '22 07:10

t0mm13b