为什么人们不这样编码?红宝石风格!C++操作符setter ruby风格
#include <stdio.h>
class Name {
public:
void operator=(char *to) {
name = to;
printf("Change!\n");
}
private:
char *name;
};
class Human {
public:
//char *name;
Name name;
};
int main() {
Human andreas = Human();
andreas.name = "Andreas";
printf("%s\n", andreas.name);
}
使用公有成员函数,直到你需要的东西添加到二传手。然后把它换成operator =类。
谁说他们没有? – ildjarn 2012-03-12 22:43:05
你好,属性。 – Xeo 2012-03-12 22:43:18
因为有公共数据成员违反封装,为一。 – 2012-03-12 22:44:41