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






















Favorite CPU:

Intel
AMD
PowerPC
Alpha
UltraSparc
Transmeta
Others

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



Linux HOWTOs  |  HOWTOs Index



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

10. Pointers are problems

Pointers are not required for general purpose programming. In modern languages like Java there is no support for pointers (Java internally uses 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 an application without using pointers. You should pointers only in those cases where references will not work.

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

 
 
 
 
 


Sylpheed 0.8.6
Astaro Security Linux 3.212
Linux Kernel 2.4 pre-patch 2.4.20-rc2
Gnumeric 1.1.12
Doxygen 1.3-rc1
QT 3.1.0
LilyPond 1.7.7
ALSA driver 0.9.0rc6
sysstat 4.0.7
FOX 1.1.20
ImageMagick 5.5.1-6



Cyrus IMAP server 2.1.10
METAGRAF-3D 1.2.2
East-Tec DiskSanitizer 2.0
autoconf 2.56
wcd 3.1.0
multi gnome terminal 1.6.1
BananaPos 0.9.6
xscreensaver 4.06
Smalltalk 2.0.8
ccaudio 1.0.4
ccscript 2.3.3
InfoNES v0.79J
dbtcp 0.1.15
SAINT 4.1.1