class myFun(Employee obj, String attribute)
{
//return proper attribute value without using conditions like If-Else, ternary or
// conditional operators like && etc.
}
,如果我现在拨打:C++对象与属性名称输出作为字符串
myFun(obj, “name”);
然后这个功能应该雇员的从对象返回名“OBJ”,这是作为参数传递。所以根据属性值的名称,它应该返回该对象的属性值。
有没有办法在C++中做到这一点,而不使用条件或开关语句?我知道在Python中我们可以使用getattr。
- 框架?在C++中没有'Object'。 Employee如何定义? – 2013-02-28 11:11:03
如果需要按名称访问属性,则应考虑使用“std :: map”。 – Axel 2013-02-28 11:13:53
对不起,对象部分..其基本上是一个类 – 2013-02-28 11:19:37