On Saturday, May 7, 2016 at 1:33:23 PM UTC-4, Rosario19 wrote:
> but structs and classes have to be address for example
>
> clasA w;
>
> than w is a pointer to the obj defined from the clasA
>
> [if that obj has not memory for to be create from the system
> than this if print no mem for w
> if(w==0) print("No Mem for w");
> ]
That's the difference between using pointers and native objects. With
a pointer there is an allocation step, and it does introduce the potential
for a failed allocation. When you use a native object, it is already
allocated by the time you are able to use it in your program.
By reference variables give you the same ability. By the time you're
able to use them, you know they are valid because the error would've
already occurred before you were able to use them in your line of source
code.
> this can be ok in using of operators as in
>
> a=b+c;
>
> so there is no reason for the reference....
What does this mean (a=b+c) when a, b, or c are something other than
a fundamental data type? In that case their use requires the class
definition to make it have contextual sense. And as such, the needs of
that mechanism at runtime require that a reference to the object be
available for the context, otherwise they'd be converted to pointers
which does introduce the possibility of a NULL and does then require
the extra protection against NULL pointers, which is one of the things
the use of references was created to avoid.
-----
There are cases where you know you have valid data. There's no reason
to then use it as a pointer because it makes the syntax more complex,
and it requires that you consistently guard against NULL values in the
pointer. With references, all of that disappears and your code is both
cleaner, and faster, because you can remove the requisite NULL checks.
Best regards,
Rick C. Hodgin