What are the difference between * and & for objects in c++ [duplicate] - c++

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 2 years ago.
First of all
I know the differences between & and * for variables but not for the objects of a class
consider I have a class let's call it a "Math"
Math math; // a simple object called math
Math* mathPtr = &math; // this will holds the address of math object
Math& mathRef = math ; // this one is exactly equivalent to the previous one
What are the differences ?!

A very big difference between pointers(*) and references(&) in nearly every situation is that a pointer is an indepedent variable and can be assigned NEW address values; whereas a reference, once assigned, can never refer to any new object until the variable goes out of scope.
Moreover a pointer can be of a null value, and a reference dosen't.

Related

What happens under the hood in any C++ compiler with reference variables? [duplicate]

This question already has answers here:
What happens after C++ references are compiled?
(2 answers)
How references are internally stored in c++? [duplicate]
(3 answers)
Closed 1 year ago.
When we declare a reference variables int & x = something, is the & just a syntactical shorthand in the source code for referencing the value of the variable something? What I mean is, does the compiler insert, under the hood, the necessary calls for pointers in order to access the value of something, or does it use some more direct approach? E.g. do we have that int x & = something is equal to int* x = &something, and using the value at x like x = 10 results in the code *x = 10?

How can a pointer point to a reference variable? [duplicate]

This question already has answers here:
Is there any way to find the address of a reference?
(10 answers)
Closed 2 years ago.
I am working with pointers and reference variable in C++.
int i=43;
int &refi=i;
What I know is the variable refi is bound to variable i. However it does not have its own memory. It is just a another name to the variable i. Then how can a pointer point to such unstored variable.
#include<iostream>
int main(){
int i=43;
int &refi=i;
int *p=&refi;
std::cout<<p;
return 0;
}
However I am not getting any error for the above code. Instead I am getting address of it.Am I wrong with the concept of reference variable here?If yes how?
The output is
0x61ff04
The reference has the same address in memory as the variable it's referencing. That essentially makes it an alias. Thus, when you take the address of a reference, you get the address of the original variable.

What's the difference between accessing struct members by pointer as opposed to by reference? [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
When to use references vs. pointers
(17 answers)
Closed 8 years ago.
In "The C++ Programming Language (4th Edition)" Section 2.3.1, Stroustrup shows 3 different ways to access members of a struct:
void f(Vector v, Vector& rv, Vector* pv)
{
int i1 = v.sz; // access through name
int i2 = rv.sz; // access through reference
int i4 = pv->sz; // access through pointer
}
I understand that for the first one, v is passed-by-value, so a copy of the first argument is put on the function's stack and the value of its size sz is stored inside of i1.
In the second example, rv is a reference to the struct passed as the second argument. Because it is a reference, we can access the value referred to by rv without a * prefix.
I'm not too sure I understand what's going on with i4, and why someone would pick the third example over the second (or vice-versa).

difference between & and * declaration [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 9 years ago.
To elaborate the title,
what is the difference between
book& a = b;
and
book* a = &b;
After learning C, these class declaration is really confusing me. Can anyone explain how these two line of codes work individually?
first one the reference variable and second one is pointer variable.
book& a = b;
meaning of above statement is:
variable "a" referencing same memory of variable "b". so if value of "b" changes it's automatically reflect to "a" and vice versa.
book *a=&b;
means variable "a" is pointer variable it will store address of "b".
Difference for reference variable memory will be same for both variable, but in pointers memory for both variable will be different.

C C++ difference between & an * [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Pointer vs. Reference
I recently started to 'relearn' c++ and encountered a simple question that i always had.
Int *intp = new int(10);
Int& intref = *intp;
intref prints as 10
*intp does so too.
Also do the prints of &intref and intp equal.
Long story short. Is the difference between & and * simply the way you access the value and adress?
Or is there a major difference in usage?
The * notation means what's being pass on the stack is a pointer, ie, address of something. The & says it's a reference.
Refer this Thread