Skip to main content
Commonmark migration
Source Link

rectTwo differs from rect in that rect is an instance of a Rectangle on the stack and rectTwo is the address of a Rectangle on the heap. If you pass a Rectangle by value, a copy of it is made, and you will not be able to make any changes that exist outside of the scope of changestuff().

Passing it by reference means that changestuff will have the memory address of the Rectangle instance itself, and changes are not limited to the scope of changestuff (because neither is the Rectangle).

Edit: your comment made the question more clear. Generally, a reference is safer than a pointer.

From Wikipedia:

It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

 

Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

 

References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.

 

References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Additionally, objects allocated on the heap can lead to memory leaks, whereas objects allocated on the stack will not.

So, use pointers when they are necessary, and references otherwise.

rectTwo differs from rect in that rect is an instance of a Rectangle on the stack and rectTwo is the address of a Rectangle on the heap. If you pass a Rectangle by value, a copy of it is made, and you will not be able to make any changes that exist outside of the scope of changestuff().

Passing it by reference means that changestuff will have the memory address of the Rectangle instance itself, and changes are not limited to the scope of changestuff (because neither is the Rectangle).

Edit: your comment made the question more clear. Generally, a reference is safer than a pointer.

From Wikipedia:

It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

 

Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

 

References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.

 

References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Additionally, objects allocated on the heap can lead to memory leaks, whereas objects allocated on the stack will not.

So, use pointers when they are necessary, and references otherwise.

rectTwo differs from rect in that rect is an instance of a Rectangle on the stack and rectTwo is the address of a Rectangle on the heap. If you pass a Rectangle by value, a copy of it is made, and you will not be able to make any changes that exist outside of the scope of changestuff().

Passing it by reference means that changestuff will have the memory address of the Rectangle instance itself, and changes are not limited to the scope of changestuff (because neither is the Rectangle).

Edit: your comment made the question more clear. Generally, a reference is safer than a pointer.

From Wikipedia:

It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.

References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Additionally, objects allocated on the heap can lead to memory leaks, whereas objects allocated on the stack will not.

So, use pointers when they are necessary, and references otherwise.

added 972 characters in body; added 66 characters in body; added 122 characters in body
Source Link
danben
  • 83.8k
  • 18
  • 127
  • 149

rectTwo differs from rect in that rect is an instance of a Rectangle on the stack and rectTwo is the address of a Rectangle on the heap. If you pass a Rectangle by value, a copy of it is made, and you will not be able to make any changes that exist outside of the scope of changestuff().

Passing it by reference means that changestuff will have the memory address of the Rectangle instance itself, and changes are not limited to the scope of changestuff (because neither is the Rectangle).

Edit: your comment made the question more clear. Generally, a reference is safer than a pointer.

From Wikipedia:

It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.

References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Additionally, objects allocated on the heap can lead to memory leaks, whereas objects allocated on the stack will not.

So, use pointers when they are necessary, and references otherwise.

rectTwo differs from rect in that rect is an instance of a Rectangle on the stack and rectTwo is the address of a Rectangle on the heap. If you pass a Rectangle by value, a copy of it is made, and you will not be able to make any changes that exist outside of the scope of changestuff().

Passing it by reference means that changestuff will have the memory address of the Rectangle instance itself, and changes are not limited to the scope of changestuff (because neither is the Rectangle).

rectTwo differs from rect in that rect is an instance of a Rectangle on the stack and rectTwo is the address of a Rectangle on the heap. If you pass a Rectangle by value, a copy of it is made, and you will not be able to make any changes that exist outside of the scope of changestuff().

Passing it by reference means that changestuff will have the memory address of the Rectangle instance itself, and changes are not limited to the scope of changestuff (because neither is the Rectangle).

Edit: your comment made the question more clear. Generally, a reference is safer than a pointer.

From Wikipedia:

It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.

Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.

References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Additionally, objects allocated on the heap can lead to memory leaks, whereas objects allocated on the stack will not.

So, use pointers when they are necessary, and references otherwise.

Source Link
danben
  • 83.8k
  • 18
  • 127
  • 149

rectTwo differs from rect in that rect is an instance of a Rectangle on the stack and rectTwo is the address of a Rectangle on the heap. If you pass a Rectangle by value, a copy of it is made, and you will not be able to make any changes that exist outside of the scope of changestuff().

Passing it by reference means that changestuff will have the memory address of the Rectangle instance itself, and changes are not limited to the scope of changestuff (because neither is the Rectangle).