「一般的な」方法でメンバー属性を設定することは可能ですか? 私はまだ c++ に不慣れで、テンプレートに飛び込んだだけですが、これが正しい方法でしょうか?
私が使用しなければならないクラスには、informix データベースから入力される約 20 の文字列メンバーがあり、フィールド (= 属性) 名を持つ配列をループできます。
単純なクラスがあるとしましょう
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() );
}