Of course, until you start using them in functions, both pointers and references usually seem to have little, er, point. The classic use of pointers is in a swap function. Suppose you want to write a function that has the potential to operate on its arguments, permanently changing their values. You will need to use pointers or references. Here’s the pointer version:void swap_with_ptrs(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp;}Here is the version that uses references:void swap_with_refs(int &aRef, int &bRef) { int temp = aRef; aRef = bRef; bRef = temp;}These two functions look similar and in fact do the same thing.I’ve gotten into trouble with one or two people by saying this, but in my opinion, both of these version implement what, in other languages, you would call pass by reference; they do what in BASIC or FORTRAN you would do by specifying a reference variable, which lets the function or subroutine change the value of the argument passed to it. In C, using pointers was the only way you could “pass by reference.”Technically, what’s really going on with the pointer version is that it’s passing pointers by value, but the effect is exactly the same as passing integers by reference.Moreover—and I will go out on a limb to say this—to implement the reference version, the compiler will almost certainly use pointers and de-reference them under the cover. Why? Because there’s no other way to do it, at least no obvious way.But... if the two versions produce exactly the same effects at runtime, how do you choose between them?I’m going to come down on the side of using references, because even though the implementations are identical, there is one other difference: When you call the function at runtime, the version with references passes the variables directly, rather than making you use the additional operator needed to get the addresses (&). So, it involves slightly less syntax.int big = 100;int little = 1;swap_with_ptrs(&big, &little); // swap big and littleswap_with_refs(big, little); // swap again!References were originally added to the C++ language to make it easy and efficient to write copy constructors. But, in addition for functions like swap, I would recommend using references as much as possible rather than pointers.Pointers aren’t going to go away any time soon, because C++ code often contains thousands of lines of C legacy code. Furthermore, C++ programmers are just in the habit of using them.
C++
Topic: Pointers
Where They get Useful Function Arguments ?
Browse random answers:
What is Pointers in c++ ?
What is importance of const. pointer in copy constructor?
Explain "passing by value", "passing by pointer" and "passing by reference"
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .
Explain which of the following declarations will compile and what will be constant - a pointer or the value pointed at: * const char ** char const ** char * const
Why Pointers are not used in C Language. What are the main differences between C and C++
Explain about Reference operator (&) ?
Explain about declaring variables of pointer types with example ?
Explain about pointers and arrays ?
Explain about Pointer initialization ?
Explain about Pointer arithmetics ?
Explain about Pointers to pointers ?
Explain the void pointers with example ?
What is Null pointer ?
What is Pointers to functions ?
What Are Pointers?
What are Using Pointers in C++ ?
Explain Accessing the Value at the Memory Address held by a Pointer ?
Why Use Pointers?
What are C++ Pointer Syntax ?
Explain about Taking Stock of Pointers ?
What are pointers? Why should you care?
Explain about Manipulating Data with Pointers and example ?
What are Array of pointers ?
Where do we use pointers?
Why do we need pointers?
Allocating a memory from the "Free Store" ?
Explain about Allocating a memory with new operator ?
Explain about De-allocating a memory with delete operator ?
Explain about Data type class pointers with example ?
Explain about memory leaks associated with pointers ?
Explain about memory leak caused by pointer reassignment ?
When local variable’s name is same as member’s name
What are function Pointer Syntax ?
What are Reading Function Pointer Declarations ?
What are Using a Function Pointer ?
What are Function Pointers in the Wild ?
How to Using Polymorphism and Virtual Functions Instead of Function Pointers (C++) ?
What are Benefits of Function Pointers ?
Where They get Useful Function Arguments ?
Explain Smart Pointers (Modern C++) ?
What are Uses for smart pointers ?
What are Kinds of Smart Pointers ?
What are another Use of Pointers Efficient Array Processing ?
Explain about C++ Pointers and new
What are problems with pointers ?
Explain about generic smart pointer class ?
How to initialize a vector of pointers?
Explain about c++ Pointer Arithmetic ?
Explain about incrementing a Pointer ?
Explain decrementing a Pointer ?
Explain pointer Comparisons ?