★ wanayoo — archive 1999 http://icewalkers.com/doclib/howtos/C++Programming-HOWTO-5.htmlNouvelle recherche | Portail wanayoo
Name:   Pass:    
Subscribe to IceWalkers Free NewsLetter




















resource exchange and knowledge database for Linux & other *nix
see all



You'll upgrade to Linux Kernel 2.4 ...

Soon
Later
Never
Already did

  View results
  Previous polls
 
 Search Howtos: Match: Sort by:



Linux HOWTOs  |  HOWTOs Index



C++ Programming HOW-TO: Pointers are problems Next Previous Contents

5. Pointers are problems

Pointers are not required for general purpose programming. In modern languages like Java there is no support for pointers!! Pointers make the programs messy and programs using pointers are very hard to read.

Avoid using pointers as much as possible and use references. Pointers are really a great pain. It is possible to write a application without using pointers.

A reference is an alias; when you create a reference, you initialize it with the name of another object, the target. From the moment on, the reference acts as an alternative name of the target, and anything you do to the reference is really done to the target.

Syntax of References: Declare a reference by writing the type, followed by the reference operator (&), followed by the reference name. References MUST be initialized at the time of creation. For example -


        int             weight;
        int     & rweight = weight;

        DOG             aa;
        DOG & rDogRef = aa;

Do's of references -

  • Do use references to create an alias to an object
  • Do initialize all references
  • Do use references for high efficiency and performance of program.
  • Do use const to protect references and pointers whenever possible.

Do not's of references -

  • IMPORTANT: Don't use references to NULL objects !!!!
  • Don't confuse the address of operator & with reference operator !! The references are used in the declarations section (see Syntax of References above).
  • Don't try to reassign a reference
  • Don't use pointers if references will work
  • Don't return a reference to a local object
  • Don't pass by reference if the item referred to may go out of scope


Next Previous Contents


HOWTOs Index

 
  
 
 
 


Ethereal 0.8.16
Linux Kernel pre-patch 2.4.3-pre2
cdrecord 1.10a16
fetchmail 5.7.2
sysstat 3.3.6



FreeAmp 2.1.rc6
Wine 20010305
autoproject 0.11.3
mapm 3.80
SkipStone 0.7.1
Moleskine 0.6.2
gefax 0.4.1
eZ publish 1.0.5
eXtrans 1.2.0
jpg2pdf 1.1
util-linux 2.11
Zircon 1.18.248
PeeWeeLinux 0.61.0
vile 9.2i
FIDOGATE 4.4.3
LilyPond 1.3.135
Knetfilter 2.1.0
MPC 1.6
maildrop 1.3.1
libfbx 0.2.0