> When I make a function CA::MyFunc(CB& b), does I have to test the address
> b before doing something?
> I think that is the responsibility of the calling client. But on the other
> side, the calling client can give a reference to a not existing object,
> without testing it.
The client *cannot* give a reference to a nonexistent object, that's the
whole point of a reference. Alright, there are some rare cases where it's
possible to invalidate a reference, but these are almost always an obscure
programming error - not to mention that there's no way to check a reference
for validity. If you passed the *address* of an object, there's a chance
that the pointer is invalid, but in general, you have no way of testing that
either. Only dereferencing the pointer will reveal that it does not point to
a valid object, but by the time you're dereferencing it it's usually too
> CA A;
> CB* pB;
> pB = NULL;
> Now an exception occurs in MyFunc.
No. The code's behavior is undefined because it attempts to dereference a
null pointer. This is obviously a fault of your client, since that *client*
is dereferencing the pointer. MyFunc will not even get called in most
instances (most implementations will die nicely with a 'null pointer
reference' message or a protection fault or something nifty like that).
There's no need to mess around with pointers if you're passing references,
though. If you had written
you'd have a hard time screwing anything up.
Quote:> Which principle does I have to handle?
Just assume the reference is valid, and leave it to the calling code not to
make the obscure mistakes that would lead to an invalid reference.