No, this is unsafe. Because std::string
's members are not the same as demo
.
But you can define a constructor to implicitly convert std::string
to demo
type.
#define MAX_SIZE 100
struct demo
{
demo(const std::string& str)
{
memset(pr00, 0, MAX_SIZE);
if (str.length() < MAX_SIZE)
{
strncpy(pr00, str.c_str(), str.length());
pr00[str.length()] = 0;
}
}
char pr00[MAX_SIZE];
};
Now you can write code as this:
std::string name("hello world");
demo d(name);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…