On Tue, 14 Aug 2007, Aravindhan Krishnan wrote:
> Date: Tue, 14 Aug 2007 17:13:55 +0530
> From: Aravindhan Krishnan <[EMAIL PROTECTED]>
> Reply-To: <[email protected]>
> To: <[email protected]>
> Subject: [twincling] C++ query
>
> Can a reference be created for a function..?
>
> Say..
> int (&fp) (int) for int fun(int);
>
> Does it have any advantage over function pointers..?
>
>
> thanks
> Aravindhan.
>
>
I have found references to be much more type safe than pointers.
Any potential null assignment situations are flagged as errors.
For the technically oriented, here is an exerpt from the wikipedia -
C++ references differ from pointers in several essential ways:
* It is not possible to refer to a reference object directly after
it is defined; any occurrence of its name refers directly to the
object it references.
* As a consequence of the first point, neither arithmetic, casts,
nor any other operation can be performed on references except
copying their binding into other references.
* Once a reference is created, it cannot be later made to reference
another object; we say it cannot be reseated. This is often done with
pointers.
* References cannot be null, whereas pointers can; every reference
refers to some object, although it may or may not be valid.
* References cannot be uninitialized. Because it is impossible to
reinitialize a reference, they must be initialized as soon as they
are created. In particular, local and global variables must be initialized
where they are defined, and references which are data members of class
instances must be initialized in the initializer list of the class's
constructor.
Please, take a look at
http://en.wikipedia.org/wiki/Reference_(C++)
Another overlooked but very important aspect has been highlighted
on Page 282, Sec 11.6, Line 13 therein in C++ Prog 3rd Edn
"References allow the use of expressions involving the usual
arithmetic operators for large objects without excessive copying.
Pointers cannot be used because it is not possible to redefine
the meaning of an operator applied to a pointer".
Hope this helps.
thanks
Saifi.