Thanks again Erik, and Andre.

André Pang <[EMAIL PROTECTED]> writes:

> The C++ FAQ Lite has a section devoted to const correctness:
>
>   http://www.parashift.com/c++-faq-lite/const-correctness.html
>
> It's an invaluable resource to bookmark if you do any C++ coding.

Cheers.

>> Do people do that in practice? Is it good practice?
>
> Most people don't bother using const for function parameters that are
> primitives (ints, bools, char, etc).  I still do, since I think it's
> good documentation: it indicates you never intend to modify it in the
> function, and frees up your mental stack to track something else.

hmmm, think I'll follow the crowd ;)

> If you're passing a large structure to a function, the standard C++
> idiom is to pass a const reference rather than pass the structure by
> value, so instead of this:
>
>   void foo(my_large_structure_t bar);
>
> do this:
>
>   void foo(const my_large_structure_t& bar);
>   
> Using a reference prevents a copy of the large structure, which can
> be relatively expensive.  Using a const reference means that you
> can't change the original structure, so it gives more similar
> semantics to pass-by-value.  It also theoretically enables more
> optimisation opportunities, although in practice it's not a big win.

I think this clears up some confusion. Thinking back, it was a const
reference. I forgot the reference bit and thought if you make a struct
a const, why not primitives...it's all good now. Thanks.

_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

Reply via email to