是否有可能在一个“通用”的方法来设置一个成员属性? 我还是新的C++和刚刚跳入模板,如果这是要走的路?设置成员属性在“通用”的方式
I类必须使用具有大约20线绳构件被从Informix数据库填充,我可以通过与字段(=属性)名称的数组循环。
比方说,我有一个简单的类
class Foo
{
public:
attr1
attr2
Foo() { };
~Foo();
}
,我可以用它这样的:
Foo foo;
string myattr = "attr1";
string myval = "val x1";
string myval = "val x2";
setattribute(foo, myattr, myval1); // pseudocode... possible somehow?
cout << foo.attr1; // prints "val x1"
setattribute(foo, myattr, myval2); // pseudocode... possible somehow?
cout << foo.attr1; // prints "val x2"
的方法我在循环调用看起来是这样的......
// its_ref : empty string reference
// row: ptr on the current db row = query result object
// colname: the db column = attribute
// ki: the object
void get_fd(ITString & its_ref, ITRow * row, ITString colname, ns4__SOAPKunde& ki) {
ITConversions *c;
ITValue *v = row->Column(colname);
v->QueryInterface(ITConversionsIID, (void **) &c);
c->ConvertTo(its_ref);
// here is the place i want to use it :
setattribute(ki, colname, its_ref.Data());
}
你可以做到这一点。你只需要一个代码生成器来为你生成反射。 – 2011-02-14 18:11:21