R
ruffiano
Does the string class have a method for retrieving the address of a
string?
Thanks.
string?
Thanks.
ruffiano said:Does the string class have a method for retrieving the address of a
string?
ruffiano said:Does the string class have a method for retrieving the address of a
string?
ruffiano said:Hi, I was trying to use the string with memcpy.
// void *memcpy( void *dest, const void *src, size_t count );
Thanks again for the quick answers. This ng is really great.
Does the string class have a method for retrieving the address of a
string?
Frederick Gotham said:ruffiano posted:
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) );
Howard said:Is that ever needed?
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.
Frederick said:Kai-Uwe Bux posted:
Yes, it's perfectly legal. The Standard explicitly states that the
following two expressions are equivalent:
int a;
double *p = &reinterpret_cast<double&>(a);
and:
double *p = reinterpret_cast<double*>(&a);
We cast to a char*, which can hold the address of any object.
So that it will compile if "T" is "int const volatile". If they ommited
the "const volatile", then the following wouldn't compile:
int main()
{
int const volatile a = 5;
int *p = addressof(a);
}
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.