Template class with unlimited parameters (Advanced)
-
I want to create a template class, with unlimited # parameters, basically I need a special template that is like a collection, but I can access each member directly. I want to create something like this class myclass : public mybaseclass { public: myclass(); DWORD myvalue1; DWORD myvalue2; DWORD myvalue3; }; I need to be able to have a different # of members "myvalue1" etc. each time. the difficult part is that the members must also be used in a function, that is part of the template also myclass::ReadMembers() { myvalue1=1; ... etc } so that I do not have to enter them again, I know macros let U do this kind of thing, but they do not work to well for this application so far. Templates are foweful, but not enough, I even looked at BEGIN_MESSAGE_MAP macro from MFC, because that idea might do it, but it can't as far as I can see.:confused: GRRR.... Maybe I need a 4GL language, not C++. Conrad - conradb@adroit.co.za Always do badly to start off, that way when you get the hang of it suddenly, everyone is surprised.
-
I want to create a template class, with unlimited # parameters, basically I need a special template that is like a collection, but I can access each member directly. I want to create something like this class myclass : public mybaseclass { public: myclass(); DWORD myvalue1; DWORD myvalue2; DWORD myvalue3; }; I need to be able to have a different # of members "myvalue1" etc. each time. the difficult part is that the members must also be used in a function, that is part of the template also myclass::ReadMembers() { myvalue1=1; ... etc } so that I do not have to enter them again, I know macros let U do this kind of thing, but they do not work to well for this application so far. Templates are foweful, but not enough, I even looked at BEGIN_MESSAGE_MAP macro from MFC, because that idea might do it, but it can't as far as I can see.:confused: GRRR.... Maybe I need a 4GL language, not C++. Conrad - conradb@adroit.co.za Always do badly to start off, that way when you get the hang of it suddenly, everyone is surprised.
Something like this should work (I don't have a compiler handy, so beware of errors):
template <int n> class C;
template <> class C<0>
{
public:
void read_members()
{
}
};
template <int n> class C: public C<n-1>
{
typedef C<n-1> super;
public:
int value;
void read_members()
{
value=1;
super::read_members();
}
};Joaquín M López Muñoz Telefónica, Investigación y Desarrollo