Next Previous Contents

5. Pointers are problems

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.

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. In languages like Java, pointers are not supported at all !!

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 not's of references -


Next Previous Contents