Bypass the overload of the address-of operator

  • Thread starter Tomás Ó hÉilidhe
  • Start date
T

Tomás Ó hÉilidhe

I can recall a little trick for getting the address of an object and
bypassing any user-defined overload of the addressof operator. I think it
was something like:

T *p = reinterpret_cast<T*>( &reinterpret_cast<char&>(obj) );

Is that it exactly, or are people using something else?
 
J

jkherciueh

Tomás Ó hÉilidhe said:
I can recall a little trick for getting the address of an object and
bypassing any user-defined overload of the addressof operator. I think it
was something like:

T *p = reinterpret_cast<T*>( &reinterpret_cast<char&>(obj) );

Is that it exactly, or are people using something else?

Almost:

// stolen from Boost:
template < typename T >
T * address_of (T & t) {
return (
reinterpret_cast<T*>(
& const_cast<char&>(
reinterpret_cast<const volatile char &>( t ) ) ) );
}



Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top