Test reference?

Test reference?

Post by Roel » Wed, 19 Jul 2000 04:00:00



Hello,

When I make a function CA::MyFunc(CB& b), does I have to test the address of
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.

CA A;
CB* pB;
pB = NULL;

A.MyFunc(*pB);

Now an exception occurs in MyFunc.

Which principle does I have to handle?

Roeland

 
 
 

Test reference?

Post by Xazzir » Wed, 19 Jul 2000 04:00:00



Quote:> Hello,

> When I make a function CA::MyFunc(CB& b), does I have to test the address
of
> 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
late :-)

Quote:> CA A;
> CB* pB;
> pB = NULL;

> A.MyFunc(*pB);

> 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

CA A;
CB B;
A.MyFunc(B);

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.

X.

 
 
 

Test reference?

Post by Jon Be » Wed, 19 Jul 2000 04:00:00



> the calling client can give a reference to a not existing object,

That's not possible, unless the caller is *deliberately* doing tricks with
pointers and references in order to be deceptive.  One of the most
important features of references is that for practical purposes, they are
guaranteed to refer to something.  You cannot create a reference without
initializing it to refer to a valid object.

--

Dept. of Physics and Computer Science        Clinton, South Carolina USA
[ Questions about newsgroups?  Visit http://www.geocities.com/nnqweb/  ]

 
 
 

1. Looking for C++ Testing References

Hi,

Are there any books related specifically to testing C++ code?  If not,
are there any general books on testing that are recommended for C++
developers?

I'd like to start seriously improving the testing of the C++ code I
write, so any other advice or references would be great.

Thanks,
Peter


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. Mac IIcx turning itself off???

3. Unit testing - references/examples

4. Accessing outlook whenm away from home

5. A reference about References to references :o)

6. Odd/Even Page Printing

7. Test Test DoNot Read [MS Test]

8. *HELP!!* -- I accidentally deleted a partition w/ fdisk -- can I recover?

9. /data1/cpp/Test.cpp:105: undefined reference to `CFileReaderEx::CFileReaderEx(char *)'

10. Testing OO systems - references.

11. need api reference(MS Win32 Developer's Reference)

12. Reference to variables & Reference to constants

13. should operator= return reference or const reference ?