[go: up one dir, main page]

0% found this document useful (0 votes)
2 views8 pages

COM221 5 C++ References

The document explains the concept of references in advanced computer programming, particularly in C++. It covers how references are initialized, their behavior in relation to target objects, and their advantages in function argument passing. It also contrasts references with pointers, highlighting their cleaner usage while noting limitations such as inability to reassign or be null.

Uploaded by

bed-com-46-22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

COM221 5 C++ References

The document explains the concept of references in advanced computer programming, particularly in C++. It covers how references are initialized, their behavior in relation to target objects, and their advantages in function argument passing. It also contrasts references with pointers, highlighting their cleaner usage while noting limitations such as inability to reassign or be null.

Uploaded by

bed-com-46-22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

COM211

Advanced Computer Programming


References
• A reference is an alias.
• A reference is initialized with the name of
another object.
• This other object is known as the target.
• Whatever you do the reference also affects
the target.
• References are created just like any other
variables.
• Declared with a type and a name.
• Variable name prefixed with a reference
operator (&).
• Same symbol as the address operator but NOT
the same operators.
• References MUST be initialized when
declared.
References
• Using the address operator (&) to
obtain the address of a reference gives
you the address of the target.
• References are aliases of variables.
• In C++ there is no way of obtaining the
address of a reference because it would
not make sense.
• References cannot be reassigned.
• Trying to reassign a reference ends up
reassigning a new value to the target.
• References cannot be null.
• MUST always refer to an existing object.
References
• Passing function arguments by
reference
• If passing arguments by value only
one value can be returned by function.
• Passing by reference enables function
to return multiple values.
• Passing by reference can be achieved
by:
• Using pointers – pass by reference using
a pointer.
• Using a reference – pass by reference
using a reference.
References
• Passing function arguments by
reference
• When an object is passed by reference it
is its address that is actually passed in
• The address of the original object is what is
put on the call stack
• Changes made to the passed in
referenced object affect the object
(target)
• Passing by reference is more efficient
than passing by value.
• Particularly when dealing with larger
objects.
• Can result in a function returning more
than one value
References
• Passing function arguments by
reference
• Const references
• Passing in parameters by reference
const can be used.
• Ensures that the original object
cannot be changed.
• Applies to both passing in
by reference using pointers
and references.
References
• Returning out of scope object
references
• Must be avoided

• Returning a reference to an
object on the heap
• Must be avoided
References
• References versus pointers
• References are cleaner and easier to use.
• Prefer references over pointers wherever
possible
• References cannot be reassigned
• Thus, use pointers of you need to reference
one object and then another
• References cannot be null
• Use a pointer if there is a chance an object
will be null.
• Be careful NOT to reference out of scope
objects.

You might also like