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
I need whatsapp software in my website Project Description: i need whatsapp software in my website same this whatsapp if anyone can make to me this in my website Skills

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Operation Files 2.1. Your program should open and read the file specified by the first command line argument of the program. 2.2. The first line of the file contains an integer

Define a structure of student class:  Write a program to define a structure of student record in C. class student { char name[20]; int roll_no; int marks[2];

#include stdio.h #include math.h float discharge(float,float); void main()   {   char prompt;      float time,out,cr;   cr = 100e-6;   for (time = 0;time   {   /* call th

how to fill the blank space in4*4 matrix?

Define Storage Classes of c program - computer programming? Each variable and function in C language has two attributes that are type and storage class. If storage class of a v


Returning References from Functions Just as in passing the parameters by reference, returning a reference also doesn't return back a copy of the variable , instead an alias is

In this Lab you will code 8086 assembler equivalents (Virgo) for common C control flow instructions. This will give you an appreciation for how control flow is implemented in assem