Re: Pointer of Reference ?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Michael Schlenger (Michi-Schlenger_at_web.de)
Date: 06/14/04


Date: Mon, 14 Jun 2004 18:11:32 GMT

On Fri, 11 Jun 2004 17:34:04 -0400, Joseph M. Newcomer
<newcomer@flounder.com> wrote:

>It also saves you from that ugly misfeature of C where you must use . for objects and ->
>for pointers (but if you get it wrong, the compiler complains. Of course it never occurred
>to the language designers that if the compiler can tell the difference, the programmer
>shouldn't need to!).

I see it exactly the other way around. A pointer is something
completely different from a reference, and you should make it damn
clear in your code what you mean. It is good that the compiler tells
you if you do something other that you mean - that is, "the compiler
is your friend", as I always say.

More general, it usually is NO GOOD IDEA to have the compiler make
assumptions about what you might have intended. Things like

        void f( int );
        f( "5" );

simply should not compile although there is an obvious semantic
interpretation of the two statements.

[snip]

> ASSERT(pDC != NULL);
> ASSERT(pBitmap != NULL);

is there any reason why you do not simply write

        ASSERT( pDC );

Do you also write

        if ( someBool == true )

?

BTW, in C++ the null pointer is noted as 0, NULL is not part of the
language, it is some library thing.

[snip]

>Some will argue that putting the & at the call site makes it obvious that the object can
>be modified, but I think this argument is specious.

This argument is completely wrong. To specify that a server does not
modify an object, the language provides the const specifier.

For some type T, the statements

        void f( const T& );
        void f( const T* );

both signal to the caller that the function cannot modify the object.
That has nothing to do with the pointer/reference thing. The good
thing of course is that the compiler can enforce this - again: the
compiler is your friend.

------------------------------------------------
Michael Schlenger
------------------------------------------------



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)
  • Re: Is there a better way?
    ... I can freely modify the contents of foo, ... Part of the issue is that C doesn't really have the full language ... Processing done by C at compile time is ... the interpreter and the compiler are permitted to ...
    (comp.lang.lisp)
  • Re: Mex Overflow Error Using Free Borland Compiler
    ... >>>would clean up a lot of code by eliminating the pointer dereferences. ... >>>guaranteed by the standard and should not be relied on. ... >>>You can try the GNU C compiler available on mingw or cygwin. ... >>>the Constraints are then both operands shall have arithmetic type. ...
    (comp.soft-sys.matlab)
  • Re: Anybody here endure C/Cpp? (.h to .inc conversion)
    ... Pascal or Stdcall convention... ... "PFNGLPOINTPARAMETERFEXTPROC" to be a type that's a pointer to a function ... DWORD, using a 32-bit compiler, with an address in it...adding the ... this is working on the premise that OpenGL does it like most others ...
    (alt.lang.asm)
  • Re: Why the compiler applies sign extension to unsigned data?
    ... I want the value of p to be made unsigned *before* the cast. ... want the cast to generate a sign extension. ... ULONG_PTR is ULONG and casting the pointer to ULONG_PTR first is the ... on the compiler and on the knowledge of the size of the ptr variable, ...
    (microsoft.public.development.device.drivers)