在“通用”属性中设置成员属性方式

发布于 2024-10-17 11:35:02 字数 1194 浏览 1 评论 0原文

是否可以以“通用”方式设置成员属性? 我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萝莉病 2024-10-24 11:35:02

您可以使用成员数据指针。这些可以是任何类型 -

struct x {
    int y;
    int z;
};

int main() {
    int x::* res = &x::y;
}

例如,但是,如果您想在运行时开始通过标识符访问它们,则必须从头开始构建自己的系统。

You can use member data pointers. These can be of any type- e.g.

struct x {
    int y;
    int z;
};

int main() {
    int x::* res = &x::y;
}

However, if you want to start accessing them by identifier at runtime, you will have to build your own system from scratch.

李不 2024-10-24 11:35:02

我能想到的唯一选择是将属性存储在 提升::任何。假设您希望属性具有异构类型。

基本思想是用 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_

冰之心 2024-10-24 11:35:02

您可以使用 std::map。
'ki' 的(基)类必须像这样实现 setattribute:

// Member variable of MyClass
std::map<string, string> mProps;

void MyClass::setattribute( const char * name, const char * value )
{
  mProps[name] = value;
} 

You could use a std::map.
The (base) class of 'ki' then has to implement setattribute like this:

// Member variable of MyClass
std::map<string, string> mProps;

void MyClass::setattribute( const char * name, const char * value )
{
  mProps[name] = value;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文