name , .
, , ++ ( ) create/destroy/manipulation:
Person *CreatePerson(void) {
Person *result = malloc(sizeof(Person));
if (result) {
memset(result, 0, sizeof(Person));
}
return result;
}
void DestroyPerson(Person *p) {
if (p) {
free(p->name);
free(p);
}
}
void SetPersonName(Person *p, const char *name) {
if (p) {
if (p->name) {
free(p->name);
p->name = NULL;
}
if (name) {
size_t len = strlen(name);
p->name = malloc(len + 1);
if (p->name) {
strncpy(p->name, name, len);
p->name[len] = 0;
}
}
}
}
const char *GetPersonName(const Person *p) {
if (p)
return p->name;
return NULL;
}
, , ++ :
class Person {
private: std::string name;
public: const std::string& getName(void) const {
return this->name;
}
public: void setName(const std::string& newName) {
this->name = newName;
}
};
, !