ps:如果你真的坚持,这是我很久以前写的版本 说服自己,我不能比std :: string做得更好。 #include< string> class minimal_string { private: char * data; std :: size_t size; inline void allocate(size_t __size ){ size = __size; data = new char [__size]; } inline void deallocate(void){ delete [] data; } inline static void memcopy(const char * source,char * dest,size_t size){ std :: char_traits< char>: :copy(dest,source,size); } inline void copy_string(const char * str){ //警告:[没有解除分配] / * |这在构造函数中使用。因此, |不能解除分配。 * / if(str!= NULL){ std :: size_t l(std :: char_traits< ; char> :: length(str)+ 1); allocate(l); memcopy(str,data,l); } else { //也许,我们应该抛出一些东西? allocate(1); * data = 0; } } public: minimal_string(void){ 分配(1); *数据= 0; } minimal_string(const char * str) { copy_string(str); } minimal_string(const minimal_string& other){ std :: size_t l(other.length()); allocate(l); memcopy(other.data,data,l); } ~minimal_string(void){ 删除[]数据; } const minimal_string& operator =(const minimal_string& other){ if(this!=& other){ deallocate(); std: :size_t l(other.length()); allocate(l); memcopy(other.data,data,l); } 返回(* this); } void setString(const char * str){ deallocate(); copy_string(str); } std :: size_t length(void) const { return(size); // return(1 + std :: char_traits< char> :: length(data)); } 运算符const char *(void)const { return(data); } }; // minimal_string 注意,在我对异常安全编码有所了解之前,我写了这个。 因此,当某些事情发生时很可能会泄漏内存 抛出。Now, if this works then the reason would be that the compiler does not doany type checking here. I ran a test:#include <string.h>#include <stdlib.h>class char_ptr{char *value;public:char_ptr() : value(strdup("")) {}~char_ptr() {free(value);}char_ptr( const char *s ) : value(strdup(s?s:"")) {}void operator= ( const char * s ) {free( value );value=strdup( s?s:"" );}};int main ( void ) {char_ptr s( "Hello, world!\n" );printf( "%s", s );}This compiles on g++-3.4.0/Linux but segfaults. You need a cast:#include <stdio.h>#include <string.h>#include <stdlib.h>class char_ptr{char *value;public:char_ptr() : value(strdup("")) {}~char_ptr() {free(value);}char_ptr( const char *s ) : value(strdup(s?s:"")) {}void operator= ( const char * s ) {free( value );value=strdup( s?s:"" );}operator const char * ( void ) const {return value;}};int main ( void ) {char_ptr s( "Hello, world!\n" );printf( "%s", (const char*)s );}This seems to work, but is flawed as pointed out above.Now, if you need a cast anyway, why not use std::string?BestKai-Uwe Buxps.: If you really insist, here is a version that I wrote long ago toconvince myself that I cannot do better than std::string.#include <string>classminimal_string {private:char* data;std::size_t size;inlinevoid allocate ( size_t __size ) {size = __size;data = new char [ __size ];}inlinevoid deallocate ( void ) {delete[] data;}inlinestaticvoid memcopy ( const char* source, char* dest, size_t size ) {std::char_traits<char>::copy( dest, source, size );}inlinevoid copy_string ( const char* str ) {// WARNING: [no deallocation]/*| This is used in the constructor. Therefore,| no deallocation can be done.*/if ( str != NULL ) {std::size_t l ( std::char_traits<char>::length( str ) + 1 );allocate( l );memcopy( str, data, l );} else {// maybe, we should throw something ?allocate( 1 );*data = 0;}}public:minimal_string ( void ) {allocate( 1 );*data = 0;}minimal_string ( const char* str ) {copy_string( str );}minimal_string ( const minimal_string & other ) {std::size_t l ( other.length() );allocate( l );memcopy( other.data, data, l );}~minimal_string ( void ) {delete [] data ;}const minimal_string & operator= ( const minimal_string & other ) {if ( this != &other ) {deallocate();std::size_t l ( other.length() );allocate( l );memcopy( other.data, data, l );}return( *this);}void setString ( const char* str ) {deallocate();copy_string( str );}std::size_t length ( void ) const {return( size );//return( 1 + std::char_traits<char>::length( data ) );}operator const char* ( void ) const {return( data );}}; // minimal_stringBeware, I wrote this before I knew anything about exception safe coding.Therefore, it is very likely that this leaks memory when something isthrown. 这篇关于聪明的char *级......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 05:38