我正在尝试复制一个非常复杂的类,其中有我自己实现的类似向量的容器,这是我的代码:
首先,这是我实现的一个Vector:(我只提供与复制数据相关的部分,其他方法不包括在内)
template <typename T> class Vector {
private:
T *_data;
size_t _size;
size_t _cap;
public:
Vector() : _size( 0 ), _cap( 1 ) {
_data = (T *)malloc( _cap * sizeof( T ) );
}
Vector( const int &init_cap ) : _size( 0 ), _cap( init_cap ) {
_data = (T *)malloc( _cap * sizeof( T ) );
}
~Vector() {
free( _data );
}
Vector( const Vector &other ) {
_size = other._size;
_cap = other._cap;
_data = (T *)malloc( _size * sizeof( T ) );
memcpy( _data, other._data, _size * sizeof( T ) );
}
void push_back( const T &new_data ) {
if ( _size >= _cap ) {
resize( _cap == 0 ? 1 : _cap * 2 );
}
_data[_size++] = new_data;
}
void resize( int new_cap ) {
if ( new_cap <= _cap ) {
return;
}
T *new_data = (T *)realloc( _data, new_cap * sizeof( T ) );
if ( new_data ) {
_data = new_data;
_cap = new_cap;
}
}
Vector &operator=( const Vector &other ) {
if ( this == &other ) {
return *this;
}
_size = other._size;
_cap = other._cap;
_data = (T *)malloc( _size * sizeof( T ) );
memcpy( _data, other._data, _size * sizeof( T ) );
return *this;
}
注意:我也在为 C 类型字符串使用自定义字符串类型容器,它的实现方式与我的向量完全相同,但只使用 char * _data;
这里是需要复制的类:
class CMailServer {
public:
~CMailServer( void ) {
}
void sendMail( const CMail &m ) {
bool found_in = false, found_out = false;
for ( int i = 0; i < _outboxes.size(); i++ ) {
if ( m.from() == _outboxes[i].id() ) {
cout << "found in out" << endl;
found_out = true;
_outboxes[i].push_back( m );
}
}
for ( int i = 0; i < _inboxes.size(); i++ ) {
if ( m.to() == _inboxes[i].id() ) {
cout << "found in in" << endl;
found_in = true;
_inboxes[i].push_back( m );
}
}
if ( !found_out )
_outboxes.push_back( CMailIterator( m, m.from() ) );
if ( !found_in )
_inboxes.push_back( CMailIterator( m, m.to() ) );
}
CMailIterator outbox( const char *email ) const {
String wanted_email( email );
for ( int i = 0; i < _outboxes.size(); i++ ) {
if ( wanted_email == _outboxes[i].id() ) {
cout << "outbox returned: " << _outboxes[i].id() << endl;
return _outboxes[i];
}
}
return CMailIterator();
}
CMailIterator inbox( const char *email ) const {
String wanted_email( email );
for ( int i = 0; i < _inboxes.size(); i++ ) {
if ( wanted_email == _inboxes[i].id() ) {
cout << "inbox returned: " << _inboxes[i].id() << endl;
return _inboxes[i];
}
}
return CMailIterator();
}
private:
Vector<CMailIterator> _outboxes;
Vector<CMailIterator> _inboxes;
};
它包含另一个类的向量,它在另一个类中有另一个向量,它们是:
class CMailIterator {
public:
CMailIterator() : _history( 1 ), _id(), _pos( 0 ) {
}
CMailIterator( const CMail &first_m, const String &id ) : _history( 1 ), _id( id ), _pos( 0 ) {
_history.push_back( first_m );
}
CMailIterator( const CMailIterator &other ) : _history( 1 ), _id( other._id ), _pos( other._pos ) {
_history = other._history;
}
void push_back( CMail new_m ) {
CMail new_mail = new_m;
_history.push_back( new_mail );
}
const CMail &operator*( void ) const {
cout << _history[_pos].body() << endl;
return _history[_pos];
}
CMailIterator &operator++( void ) {
_pos++;
return *this;
}
private:
Vector<CMail> _history;
String _id;
int _pos;
class CMail {
public:
CMail( const char *from, const char *to, const char *body ) : _from( from ), _to( to ), _body( body ) {
}
CMail( const CMail &other ) : _from( other._from ), _to( other._to ), _body( other._body ) {
}
private:
String _from;
String _to;
String _body;
问题:
int main(){
CMailServer s0;
s0.sendMail( CMail( "john", "peter", "some important mail" ) );
s0.sendMail( CMail( "john", "amanda", "also an important mail" ) );
CMailServer s1( s0 );
// here, the code works correctly, all data is copied into s1, but is also still in s0. Here is the problem:
// We send out different emails on both servers:
s0.sendMail( CMail( "john", "mom", "mail to mom" ) );
s1.sendMail( CMail( "john", "dad", "mail to dad" ) );
// if we now test the contents of s0, it will ONLY have pushed back the `mail to dad` CMail instance...
return 0;
}