Home > Backend Development > C++ > When are Pointer and Reference Parameters Functionally Equivalent in C ?

When are Pointer and Reference Parameters Functionally Equivalent in C ?

Susan Sarandon
Release: 2024-11-03 15:56:30
Original
947 people have browsed it

When are Pointer and Reference Parameters Functionally Equivalent in C  ?

Distinguishing Pointer and Reference Parameters

In C , the concept of pointer and reference parameters often leads to confusion. This article elucidates the subtle differences between using pointers and references as function parameters, with an emphasis on their functional equivalence under certain conditions.

Pointers vs. References

The primary distinction between pointers and references lies in their semantics. A pointer is a variable that stores the memory address of another variable, while a reference is an alias or synonym for an existing variable. This difference manifests in several ways:

1. Ownership and Assignment

Pointers have explicit ownership of the data they point to. They can be reassigned to point to different memory locations or even be set to nullptr. References, on the other hand, do not explicitly own the data they refer to. Once a reference is initialized, it cannot be reassigned to another object.

2. NULL Handling

Pointers can be assigned a NULL value, indicating that they do not point to any valid memory location. References cannot be assigned NULL because they are always aliases for a variable that must exist.

3. Addressing

When you take the address of a pointer, you get the address of the pointer variable itself. When you take the address of a reference, you get the address of the object being referred to.

Functional Equivalence of Pointer and Reference Parameters

Despite their differences, pointer and reference parameters can be functionally equivalent in certain scenarios. The most notable example is when the someInt() method in your code example is not virtual. In this case, both functions essentially call the same method on the same object, regardless of whether the object is passed as a pointer or a reference.

However, when someInt() is declared virtual, the function called will depend on the actual type of the object being passed, even if the formal parameter is a pointer to the base class bar. This is because passing a reference to the base class does not change the dynamic type of the object being passed.

Referencing Through Pointers

The code snippet:

bar& ref = *ptr_to_bar;
Copy after login

creates a reference ref that refers to the object pointed to by ptr_to_bar. This is possible because the pointer operator (*) dereferences the pointer, returning the object it points to. As a result, ref and *ptr_to_bar are aliases for the same object.

The above is the detailed content of When are Pointer and Reference Parameters Functionally Equivalent in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template