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
Should my constructors employ "assignment" or "initialization lists"?

how many values we can return from pointer to function?

Flash my terminal vx670 Project Description: Have VeriFone software need it to communicate with the vx670 through com port 1 it is not talking I'm not sure why have all the r

F u nction Returning Object: This program is like to the previous program except the function returns object.  The main rule to be remembered is the function returning obj

how to write a c program for stone game

What if I cannot wrap the local in an artificial block? need help on Artificial Block in c++.

Find out the largest torque: A flat belt is taken in use to transmit torque from pulley A to pulley B as shown in figure given below. The radius of each pulley is 50mm and co

write a prgm to find minimum total number of shelves including the intial one required for loading process


how to create a shopping cart