c++中初始化和赋值操作差别是很大的。
对于基本数据类型差别不大:
比如:
int
a
=
12
;
//
initialization, copy 0X000C to a
a = 12 ; // assignment, copy 0X000C to a
a = 12 ; // assignment, copy 0X000C to a
但是对用户自定义的数据类型比如String 初始化和赋值就差别很大:
class
String
{
public:
String( const char *init ); // intentionally not explicit!
~String();
String( const String &that );
String &operator =( const String &that );
String &operator =( const char *str );
void swap( String &that );
friend const String // concatenate
operator +( const String &, const String & );
friend bool operator <( const String &, const String & );
//...
private:
String( const char *, const char * ); // computational
char *s_;
} ;
public:
String( const char *init ); // intentionally not explicit!
~String();
String( const String &that );
String &operator =( const String &that );
String &operator =( const char *str );
void swap( String &that );
friend const String // concatenate
operator +( const String &, const String & );
friend bool operator <( const String &, const String & );
//...
private:
String( const char *, const char * ); // computational
char *s_;
} ;
初始化的构造过程比较简单:先分配一个足够大的空间然后填充上数据:
String::String(
const
char
*
init )
{
if( !init ) init = "";
s_ = new char[ strlen(init)+1 ];
strcpy( s_, init );
}
if( !init ) init = "";
s_ = new char[ strlen(init)+1 ];
strcpy( s_, init );
}
析构过程更简单:
String::
~
String()
{ delete [] s_; }
但是如果赋值操作就复杂多了:
String
&
String::
operator
=
(
const
char
*
str )
{
if( !str ) str = "";
char *tmp = strcpy( new char[ strlen(str)+1 ], str ); // 多了中间变量
delete [] s_; // 多了删除s_;
s_ = tmp; // 多一个赋值操作!现在是指向字符的指针,如果是个大对象,效率的差别可想而知.
return *this;
}
if( !str ) str = "";
char *tmp = strcpy( new char[ strlen(str)+1 ], str ); // 多了中间变量
delete [] s_; // 多了删除s_;
s_ = tmp; // 多一个赋值操作!现在是指向字符的指针,如果是个大对象,效率的差别可想而知.
return *this;
}
建议在条件允许的情况下最好在初始化的时候就赋值,而尽量避免用=号赋值了,比如用成员初始化列表来初始化成员数据,不在构造函数里用赋值操作给成员数据.