1

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() );
}
1
  • 1
    You can do that. You just need a code generator to generate reflection for you. Feb 14, 2011 at 18:11

3 Answers 3

2

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.

3
  • 1
    +1 For a different solution. This can be easily extended to runtime... std::map< std::string, int x::* > can be used to map any string to any member (of type int), then when data is being introduced it can be set as: obj.*attributes[name] = value; Not the nicest solution though... Feb 14, 2011 at 19:25
  • So if my object of type x has attribute obj.z = 3; i need to access it via cout << obj.*attributes["z"]; which does not work. could you explain further please? Feb 15, 2011 at 11:04
  • ok got it, also from here i need to attributes["z"]=&x::z; first. Feb 15, 2011 at 11:22
1

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_

1
  • I have read about it, it's nice. But you don't want to change the implementation of SOAPKunde class , means the ki object ? I have no influence on that. Or did i get you wrong? Feb 14, 2011 at 16:25
0

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;
} 
3
  • this is not what he talks about. Feb 14, 2011 at 16:08
  • @Nawaz: Why so sure? I think he is pretty new to C++ and told pretty vague, what he wants (e.g. dived into templates, foo = Foo(); ???). Do you know it that exact?
    – Valentin H
    Feb 14, 2011 at 16:36
  • It's right, i have an object and want to set the members attributes, cant use a map here. - Thx Valentin for pointing out my foo init mistake, i updated above. (i was used to python up to know...) Feb 14, 2011 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.