difrent between c& and c*

i have this lecture that in it we have a class named c.
in one of it's methods it return c&.
what is the difference between c& and c* ?
what is c& exactly?
here's the code example:

 
  c& c::add (int n) {x = n; return *this};


it might return value
so why do we use & ?
it might return value so why do we use & ?

You would return a reference if you want the user to be able to modify the result. Returning a pointer always begs questions: is the returned object on the heap? If so then who is responsible for deleting it? How long is it valid? Can it return a nullptr?

If the object is large then you might return a const reference rather than a value for efficiency. It lets you avoid constructing and quickly destroying a temporary object.
Simple example of useful reference return:
std::cout << "Hello " << name << std::endl;
Each operator<< takes and returns a reference to ostream.

Lets write named functions for same:
ostream & print( ostream &, T );
(Many overloads, each with different T.)

Now we write the example again:
print( print( print(std::cout, "Hello "), name), std::endl);
The thing is that each output command changes the std::cout, there is only one std::cout, and you really like to chain those operators, rather than to write:
1
2
3
std::cout << "Hello ";
std::cout << name;
std::cout << std::endl;

That is why reference is returned here.

Pointer is a variable that holds a number that is assumed to be a memory address. Some memory is used to store the address.

Reference is name alias. Alias does not use memory, it is just communication between developer and compiler.
allrigh thank u guys
one more question
if c& return reffrence then whats the diffrence between c& and c* ?
mutexe did post a link that describes both references and pointers. Did you read it?
Consider this:
1
2
3
4
5
6
int *p; // p is a pointer to int
int x = 0; // x is an int
p = &x; // p is now pointing to x
int &r = x; // r  is a reference to x
int y = 0;
p = &y; // p is now pointing to y 

We start off declaring p, but not initialising it. We can change the value of p, so it points to x, we can also change it again, so it points to some other int (y).
r is a reference to x. We can never make r refer to anything else, it is a reference to x forever.
r cannot be naked, it must always refer to something. You can have a dangling pointer, like
int *p;
But you can not have a dangling reference like:
int &r; // no!
To use or change the value pointed to you need to de-reference a pointer:
1
2
3
int x = 0;
int *p = &x; // notice you assign the address of x to the pointer, not x itself
*p = 1; // de-reference the pointer, now x == 1; 

To use a reference, there is no de-referencing semantic
1
2
3
int x = 0;
int &r = x; // notice you don't have to use the address of x 
r = 1; // no need to de-reference, now x == 1 

If it helps, you may think of a reference as a pointer that is automatically de-referenced

Note you can't make a reference refer to something else. For example:
1
2
3
4
int x = 1;
int &r =x;
int y = 2;
r = y; // this does not make r refer to y, it just assigns the value of y to x, now x == y 
Topic archived. No new replies allowed.