Topic: What does &* really mean?


Author: Gary Brown <gbrown@thebrowns.ultranet.com>
Date: 1996/09/03
Raw View
James,

> |> I frequently use "&*" to pass pointers to functions by reference.  My

> I presume you mean *&, e.g. f( int*& rpi ).

Yes, I did mean *&.  I rarely used that form but older Borland compilers
usually did what I wanted.  Now, at least, I understand a relevant
comment in Meyers' "(More) Effective C++" book.

Thanks,
Gary
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Gary Brown <gbrown@thebrowns.ultranet.com>
Date: 1996/08/29
Raw View
Hi,

I frequently use "&*" to pass pointers to functions by reference.  My
intent is to update the pointer without having to return it explicitly
or using "**."  (Counting asterisks throughout a functions is error
prone).

However, my Borland 4.02 compiler sometimes copies the pointer to a
temporary and the temporary receives any changes.  This not what I
intend.  Am I misunderstanding something or is this a bug in the
compiler.

Thanks,
Gary


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/08/29
Raw View
In article <32253145.4353@thebrowns.ultranet.com> Gary Brown
<gbrown@thebrowns.ultranet.com> writes:

|> I frequently use "&*" to pass pointers to functions by reference.  My
                     ^^

I presume you mean *&, e.g. f( int*& rpi ).

|> intent is to update the pointer without having to return it explicitly
|> or using "**."  (Counting asterisks throughout a functions is error
|> prone).

|> However, my Borland 4.02 compiler sometimes copies the pointer to a
|> temporary and the temporary receives any changes.  This not what I
|> intend.  Am I misunderstanding something or is this a bug in the
|> compiler.

It is always a violation of the standard (unless the pointer is const,
e.g.: "f( int*const& rpi )").  However, it may not be what you think.

If the type of the argument is not the same as the type of the
parameter, then the compiler must make a conversion.  The result of this
conversion is not an lvalue; when bound to a reference, this means that
it is a temporary which gets bound.  Thus:

    char*           p ;
    void f( char const*& rp ) ;
    f( p ) ;                    //  conversion char* ==> const char*

or

    unsigned*       p ;
    void f( int*& rp ) ;
    f( p ) ;                    //  conversion unsigned* ==> int*

The use of the temporary surprises many people and results in errors
that are difficult to find.  For this reason, it was made illegal to
bind an rvalue to a reference to a non-const.  (Presumably, if the
reference is to a const, you won't be modifying the object in question,
and so it is indifferent whether it is a temporary, or the actual
object.)  This change is quite ancient now; the ban was already in the
ARM.  Still, many compilers continue to allow it, and generate the
temporary, in order to avoid breaking existing code.

Most such compilers will generate a warning in such cases.  Have you got
warnings turned on, and are you looking at them?
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils,    tudes et r   alisations en logiciel orient    objet --
                -- A la recherche d'une activit    dans une region francophone



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]