class myFun(Employee obj, String attribute)
{
//return proper attribute value without using conditions like If-Else, ternary or
// conditional operators like && etc.
}
现在如果我打电话:
myFun(obj, “name”);
那么这个函数应该从作为参数传递的对象“obj”中返回员工的名字。因此,根据属性值的名称,它应该返回该对象的属性值。
有什么方法可以在不使用 if 条件或 switch 语句的情况下在 c++ 中做到这一点?我知道在python中我们可以使用getattr。