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




















You visit IceWalkers.com each ...

Day
Week
Month
Year
First visit!

 Search Howtos:    Match:    Sort by:   



Linux HOWTOs  |  HOWTOs Index



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

11. 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

 
 
 


Ethereal 0.9.3
ImageMagick 5.4.4-1
Linux Kernel 2.4 pre-patch 2.4.19-pre5
GKrellM 1.2.11
GTK-- 1.3.9
Python 2.2.1c2



Gaim 0.55
Linuxconf 1.27r3
Nautilus 1.1.11
ExEf v100
Linux PCI Utilities 2.1.10
logwatch 2.6
libglade 1.99.10
GTK2 2.0.1
ATK 1.0.1
Pango 1.0.1
LilyPond 1.5.49
GLib2 2.0.1
SAINT 3.5.1
Alien 8.03
Komba 0.73
NcFTP 3.1.3
Achievo 0.9.0
QuickCRC Linux 1.0
lcrzo 4.07