Topic: this" as a reference
Author: jdennett@cox.net (James Dennett)
Date: Wed, 5 Apr 2006 18:03:16 GMT Raw View
NULL@NULL.NULL wrote:
>> T* AddressOf(T& x)
>> {
>> char& c = reinterpret_cast<char&>(x);
>> return reinterpret_cast<T*>(&c);
>> }
>
>
> What's that?
It was a reply to
>>> if 'this' were a reference,
>>> how would you get around overloaded operators such as operator&()
>>> or operator->()?
I.e., an explanation of how to get the address of an
object when operator& has been overloaded for the type
of that object.
> Are you trying to write a function which will return the address of an
> object which is passed to it by reference?
Yes, in special circumstances.
> template<class T>
> T* AddressOf(T& object)
> {
> return &object;
> }
This would call the overloaded operator&, which might
not return the address (and might not even return a T*).
> It's undefined behaviour to use reinterpret_cast change one pointer type
> to another. You can only change to and from "void*".
Not quite; there are other rules that allow some specific
cases other than this. This technique is used, for example,
in boost, and has been discussed at some length -- for
numerous reasons we need to be able to get a char* pointing
at an object, and to the best of my knowledge the rules *do*
allow this, though I don't have citations to hand.
-- James
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: NULL@NULL.NULL ("Tom s")
Date: Wed, 5 Apr 2006 19:00:00 GMT Raw View
> This would call the overloaded operator&, which might
> not return the address (and might not even return a T*).
Okay sorry for butting in on the conversation. I didn't know until now=20
(and presumed it would be forbidden) to overload the addressof operator.=20
At first thought it sounds disastrous -- when you stick the addressof=20
operator before a object's name, you should get the object's address!
Okay I'll admit that I'm working on first impressions here, but it seems=20
incredibly childish to overload the addressof operator just so we can do:
&some_smart_pointer_object
rather than:
some_smart_pointer_object.GetAddress()
The "addressof" operator should give us the address of the object in=20
question! At the very least, I would expect an extra operator to be added=
=20
to C++:
T* address =3D actual_address( object );
I wonder is there many people that define macros so the addressof=20
operator can't be overloaded. Something like:
#define operator& OperatorAddressOf;
Or even maybe make a universal base class, such that writing:
class String { ...
is the same as writing:
class String : public UniversalBaseClass {...
and have UniversalBaseClass defined as follows:
class UniversalBaseClass { };
That way, we could do the following:
&static_cast<UniversalBaseClass>(allegedly_smart_pointer_object);
-Tom=E1s
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]