0

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 ?!

  • 1
    Does this answer your question? [What are the differences between a pointer variable and a reference variable in C++?](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) – fas Apr 10 '20 at 10:05
  • The third one is not equivalent to the second one either. – Peter Apr 10 '20 at 10:07

1 Answers1

1

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.

Yan.F
  • 630
  • 5
  • 20