Howard wrote:
>
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:wB*******************@news.indigo.ie...
>ruffiano posted:
>>Does the string class have a method for retrieving the address of a
string?
If you don't trust a type to give you its real address, then:
string obj;
string *p = reinterpret_cast<string*>(
&reinterpret_cast<char unsigned&>(obj) );
Is that ever needed?
Rarely, but keep in mind that operator& is overloadable. So, when you have a
function
foo ( T* ptr );
and you call it like foo(&obj), it could happen that the compiler complains
that &obj is not of type T* even though obj is of type T.
More importantly, is it legal?
I think so. The boost version runs (essentially) like this:
template <typename T>
T* addressof ( T & v ) {
return reinterpret_cast<T*>(
&const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
}
I never quite understood why they put in the const volatile just to cast
ways the constness in the very next step.
Best
Kai-Uwe Bux