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

Assignment Help:

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.

 


Related Discussions:- When should i use references, and when ought to use pointers

Evaluate the credit worthiness of a client, Write a C++ program that can be...

Write a C++ program that can be used to evaluate the credit worthiness of a client. The program reads the credit limit and the price and quantity of the item to be purchased by the

Explain the switch construct, The Switch Construct The switch statement...

The Switch Construct The switch statement is a multi-way decision-making construct that tests an expression matches one of a number of constant values, and branches accordingly

PEBBLE MERCHANT, There is a pebble merchant. He sells the pebbles, that are...

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

Explain string constants in c language - escape sequences, Explain string c...

Explain string constants in c language - Escape Sequences? Illustrations are "945", "hello", "well done", "5+3". The character constant (example 'x') isn't equivalent to the st

Create a software application, Project Description: Currently seeking so...

Project Description: Currently seeking someone who can create me a software application (google chrome crx file_ that will auto add all shoe sizes to cart directly and if not av

Link list, For this program you will add and test 2 new member functions to...

For this program you will add and test 2 new member functions to the IntSLList class posted on the website. The two member functions are: insertByPosn(int el, int pos) Assuming t

Pragma, How to specify different memory sections for a variable using #Prag...

How to specify different memory sections for a variable using #Pragma?

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd