#include <cstring>
#include <iostream>
using namespace std;
struct Name
{
Name(const char * name = "")
{
int size = strlen(name);
if (size > 19) {
size = 19;
}
strncpy(m_name, name, size);
m_name[size] = '\0';
}
char m_name[20];
};
int main()
{
Name a;
cout << a.m_name << endl;
new (&a) Name("Adward Ink");
cout << a.m_name << endl;
new (&a) Name("二斗墨汁");
cout << a.m_name << endl;
return 0;
}
笔记:防止调用拷贝构造函数时的自我拷贝 中也有用过placement new