Re: using const & in function prototypes
- From: Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx>
- Date: Thu, 06 Nov 2008 08:58:00 +0100
Tommy wrote:
I am probably getting an unreasonable doubt with the VC8 compiler, so
before I go overboard with details and background, I would like to ask
this simple C/C++ question:
In code where I might have:
BOOL func(DWORD ref);
changing this to:
BOOL func(const TINT &ref);
where TINT is
#define TINT DWORD
You should use a typedef. Macros are evil, see the FAQ. Further, I don't see
how the two changes are related to each other.
Is there something I should be aware under the current VC8+ compilers
or I should "watch for" or be aware of when using this syntax that
might possible change logic in my function code block?
You are passing a reference to an object instead of a copy. That means that
if the original changes through some side effect, it will also change
inside the function. Further, if the function changes it, the changes will
be visible to the calling code. Note that it can actually change it because
it can well use a const_cast.
I ran into a situation where the only difference is the above and I am
getting different unexpected results. So before digging deeper to see
whats happening, was I correct in my long time thinking that the above
syntax was ok and proper?
Just fix the macro. Otherwise, the code doesn't bear any surprises. However,
I see no reason to use a reference here at all. The point is that
technically, the reference becomes a pointer in the compiled binary and
passing a pointer is at best as performant as passing a DWORD, but the
pointer still has to be dereferenced, which means another memory access. As
a rule of thumb, all builtin types and similarly small PODs can be passed
by value.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
.
- Follow-Ups:
- Re: using const & in function prototypes
- From: Tommy
- Re: using const & in function prototypes
- References:
- using const & in function prototypes
- From: Tommy
- using const & in function prototypes
- Prev by Date: Same code and different result, Why?
- Next by Date: Re: problem with old name project files
- Previous by thread: Re: using const & in function prototypes
- Next by thread: Re: using const & in function prototypes
- Index(es):
Relevant Pages
|