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