在“通用”属性中设置成员属性方式
是否可以以“通用”方式设置成员属性? 我对 C++ 还是新手,刚刚开始研究模板,如果这是要走的路?
我必须使用的类有大约 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() );
}
Is it possible to set a member attribute in a "generic" way?
I am still new to c++ and just dived into templates, if this is the way to go?
The class i have to use has around 20 string members to be filled from informix database and i could loop through an array with the field(=attribute) names.
Let's say i have a simple class
class Foo
{
public:
attr1
attr2
Foo() { };
~Foo();
}
and i could use it like that:
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"
The method i call in the loop could look like this...
// 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() );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用成员数据指针。这些可以是任何类型 -
例如,但是,如果您想在运行时开始通过标识符访问它们,则必须从头开始构建自己的系统。
You can use member data pointers. These can be of any type- e.g.
However, if you want to start accessing them by identifier at runtime, you will have to build your own system from scratch.
我能想到的唯一选择是将属性存储在 提升::任何。假设您希望属性具有异构类型。
基本思想是用 map 替换 Foo 中的属性。因此,您将拥有一个包装它们的映射,而不是拥有所有私有属性。 C++ 的问题是编译程序后属性名称不存在(与 python 等其他脚本语言不同)。 就无法从表示属性名称的字符串访问属性变量
因此,如果不使用某种数据结构删除旧的编辑,
The only option I can think of would be to store you attributes in a map of boost::any. With the assumption that you want your attributes to be of heterogeneous types.
The basic idea is to replace your attributes in Foo with map. So instead of having all your private attributes you would have a map that wraps them. The problem with C++ is that your attribute names don't exist after compiling the program (unlike other scripted languages like python). So there is no way to access an attribute variable from a string representing it's name without using some kind of data structure
removed old edit_
您可以使用 std::map。
'ki' 的(基)类必须像这样实现 setattribute:
You could use a std::map.
The (base) class of 'ki' then has to implement setattribute like this: