I'm not sure I should try to help someone so full of himself as you are (but I'll give it a go anyway):
hamsterman very kindly asked you to clarify yourself, not simply repeat yourself because he was to dense to get it the first time through. Further, he knows the language and its implementation at least as well as you do. The fact is that you are not making much sense.
All
extant implementations of the STL have
data() and
c_str() simply return a pointer to the internal data storage, so you can indeed do non-kosher things like modify it.
But the reason it is returned as a
const reference is because the language permits some flexibility in implementation -- which is designed to accomodate future possibilities that we may be unaware of.
In your case, however, you appear to be trying to overlay types on memory anyway (something dangerous to do as it is). Simply cast as you have done.
1 2 3 4
|
COMPLEXSTRUCT* complex_struct( std::string& s )
{
return (COMPLEXSTRUCT*)(s.data());
}
|
1 2
|
string myfoo( sizeof( COMPLEXSTRUCT ), 0 );
complex_struct( myfoo )->member = 42;
|
Remember that this assumes a POD struct, as C++ classes cannot be treated so cavalierly.
Remember also that the string will not be anything useful as a string. For conversions between 42 and "42" you'll want to look in to some proper
object serialization.
Hope this helps.
[edit] I began responding before the last two posts, but was distracted until I could hit 'Submit' until now...