Re: using const & in function prototypes



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
.



Relevant Pages

  • Re: pointer/ref question
    ... reference" to generically indicate we're conveying the address of an ... to /me/ one can describe both of these situations as "passing ... the term "pass by pointer" for the first case when teaching C++, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to pass a reference to a CComBSTR
    ... >> This is not passing by reference - you are passing pointers. ... CComBSTR tempCComBSTR); ... Approach B (passing by pointer) ...
    (microsoft.public.vc.atl)
  • Re: no pointer in Java => my problem
    ... > the value you are passing is an adress and you can't change the ... When passing a reference, you don't get ... access to the pointer. ... Neither Java nor C have anything resembling this. ...
    (comp.lang.java.programmer)
  • Re: Destructor for const object
    ... For the DbAutoPtr you cannot have the reference count in the pointer. ... What's wrong with const T*? ... > bool isNull() const; ...
    (comp.lang.cpp)
  • Re: pointer/ref question
    ... > reference" to generically indicate we're conveying the address of an ... > to have a separate term for that and passing to a function taking a ... > passing a pointer to x by value. ... I know I have the habit of saying pass by reference when a pointer is being ...
    (alt.comp.lang.learn.c-cpp)