This article has been dead for over three months
You
#include <string>
#include <cstring>
#include <iostream>
class c_string
{
std::string& cpp_str;
char* mutable_str;
c_string(const c_string&);
c_string& operator=(const c_string&);
public:
c_string(std::string& s)
: cpp_str(s)
, mutable_str( new char[s.size()+1] )
{
std::strncpy(mutable_str, s.c_str(), s.size()+1);
}
operator char*&() { return mutable_str; }
~c_string()
{
cpp_str = mutable_str;
delete [] mutable_str;
}
};
/* Example of 'C' Code to mutate a C string
* - Credit to Dave Sinkula
* - http://www.daniweb.com/code/snippet216514.html
*/
void mystrrev(char *s)
{
char *t = s;
while ( *t != '\0' ) ++t;
for ( --t; s < t; ++s, --t )
{
char temp = *s;
*s = *t;
*t = temp;
}
}
int main()
{
std::string str = "Backwards?";
std::cout << str << std::endl;
mystrrev( c_string(str) );
std::cout << str << std::endl;
}