#include <iostream> |
#include <string> |
#include <vector> |
using namespace std; |
class obj |
{ |
public : |
obj() |
{ |
m_data = new char [256]; |
printf ( "obj constructed\n" ); |
} |
obj( const obj &other) |
{ |
size_t length = strlen (other.m_data); |
m_data = new char [length + 1]; |
Strcpy(m_data,other.m_data); |
} |
obj & operator = ( const obj &other) |
{ |
if ( this == &other) |
return * this ; |
delete []m_data; |
size_t length = strlen (other.m_data); |
m_data = new char [length + 1]; |
Strcpy(m_data,other.m_data); |
return * this ; |
} |
~obj() |
{ |
delete []m_data; |
} |
private : |
char * m_data; |
}; |
int main() |
{ |
vector<obj> oo; |
obj o; |
oo.push_back(o); //在此处调用拷贝构造函数 |
return 0; |
} |