When should i use references, and when ought to use pointers, C/C++ Programming

A: Use references when you can use, and use pointers when you have to.

References are generally preferred over pointers whenever you don't require "reseating". Usually this means that references are most useful in class's public interface. Typically references appear on the skin of an object, & pointers on the inside.

The exception to the above is where a function's parameter or return value require a "sentinel" reference a reference which does not refer to an object. Usually this is best done by returning/taking a pointer, & giving the NULL pointer this special significance (references must always alias objects, not dereferenced NULL pointer).

Note: Old line C programmers sometimes don't like references as they provide reference semantics which isn't explicit in the caller's code. However, after some C++ experience, one quickly realizes it is a form of information hiding, which is an asset instead of a liability. E.g., programmers must write code in the language of the problem instead of the language of the machine.

 

Posted Date: 3/20/2013 3:36:19 AM | Location : United States







Related Discussions:- When should i use references, and when ought to use pointers, Assignment Help, Ask Question on When should i use references, and when ought to use pointers, Get Answer, Expert's Help, When should i use references, and when ought to use pointers Discussions

Write discussion on When should i use references, and when ought to use pointers
Your posts are moderated
Related Questions
The program reads two numbers and a character (operator). If the character is "+" then sum is displayed. If it is "-"then difference is displayed. If it is "*" then product is disp

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes


Task 1 Demonstrate a Java client and server exchanging one text message in each direction. Task 2 Demonstrate a C client and server exchanging one text message in each

Define Passing Structure to a Function? A structure is able to be passed as a function argument identical to any other variable. If we are merely interested in one member of a

Rules for overloading an operator This summarizes the most significant points you need to know in order to do operator function overloading. The only operators you may o

Question: (a) Explain a linked list. (b) Describe the three different types of linked list with the help of diagrams. (c) Give two advantages and two disadvantages

Write a C++ program doing the following: The program reads from the keyboard the numbers of rows and columns of a 2-D array of integers. Then, it allocates this array (let’s name i

write a C program that can display the transpose form of a ciphertext matrix.

in this problem u given a board in which some of the elements are placed as shown in diagram below .each element represent a color .fill the other elements in the board such that n