On Fri, 25 May 2007 10:37:25 +1000 Michael Chesterton wrote: > OK, I think this is where I got tripped up. I couldn't see the > difference between a pointer that gets copied, and ints, etc. > > So > > int smallest(foo &smallest, const foo f1, const foo f2);
First off, be aware that in the following I may get something wrong because this stuff is complicated. If you want to learn C or C++ I suggest that you play with these things to confirm what I or anyone else says :-). Also be aware that there are subtle differences between C anc C++. There's also one bit that I forgot in my previous email; if you have a function parameter of say "const int x" then not only can X not be modified in the calling function, it can't be modified in the destination function either. This can be of some value. > So my understanding is even though f1 and f2 gets copied, because they > can be de-referenced you make them const? It gets a little complicated here; foo could be any of the following: typedef int foo ; /* Just an interger */ typedef int * foo ; /* A pointer to an int. */ class foo ; /* Some object. */ What const means and does will be slightly different for each. For the case where foo is an integer, "const foo f1", the int will be copied and you won't be able to modify it in the destination function. For the case where foo is a pointer to int, "const foo f1", the pointer will be created and you won't be able to modify what the pointer points to in the destination function but you will be able to say increment the pointer or make it point at something else, but still not be able to modify anything it points at. In the last case, class foo, you *really* want to be aware of how big the foo object is, and what the overhead is of creating it before the function call and deleting it after. For this reason, having the function take a reference, "const foo & f1" makes a lot of sense, because no object is created or destroyed, the compler just takes the objects address and passes it to the function. Thats rather a lot of subtlty you need to have a reasonable grasp of. Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- The Earth is around 70% water. Fish rule the seas. Humans are over 90% water. It's only a matter of time. _______________________________________________ coders mailing list [email protected] http://lists.slug.org.au/listinfo/coders
