A
ardi
Hi,
I'm designing a system where some classes need to know an array of
pointers to their members (for achieving some limited RTTI
functionality).
From my C++ knowledge, my first guess is that this could be done
passing the 'this' pointer of the container class to the constructors
of the member objects, so that their constructors can call some
service in the system, telling "please register me as a member inside
this container class".
However, this approach has the annoyance that the user of this system
would need to put all to-be-registered members inside either the
initialization list of the container class constructor, or explicitly
initialize them in such constructor body... and if the user forgets to
do this, the forgotten members wouldn't be registered as members of
their container class.
Ideally, what I'd want to do is to be able to use the initialization
list syntax in the class declaration instead of in the class
constructor.
I mean, I'd love to be able to do something like this:
class MyContainer
{
public:
mydatatype value(this);
myotherdatatype anothervalue(this);
...and so on...
};
This way, I could even use a preprocessor definition to simplify even
more the system to the user.
But, AFAIK, it's not possible to initialize members in the class
declaration like this, so the user would need to _both_ declare
members in the class declaration, and initialize them with the 'this'
pointer in the container class constructor... yes, it doesn't look
like a severe annoyance, but it's an important annoyance for the
system I'm designing, where I just wish that when the user declares a
registrable data member, it's automatically registered as a member of
the class without any further lines of code.
So, I now repeat the post question: what do you think it's the
shortest way of constructing array of members of a class?
TIA
ardi
I'm designing a system where some classes need to know an array of
pointers to their members (for achieving some limited RTTI
functionality).
From my C++ knowledge, my first guess is that this could be done
passing the 'this' pointer of the container class to the constructors
of the member objects, so that their constructors can call some
service in the system, telling "please register me as a member inside
this container class".
However, this approach has the annoyance that the user of this system
would need to put all to-be-registered members inside either the
initialization list of the container class constructor, or explicitly
initialize them in such constructor body... and if the user forgets to
do this, the forgotten members wouldn't be registered as members of
their container class.
Ideally, what I'd want to do is to be able to use the initialization
list syntax in the class declaration instead of in the class
constructor.
I mean, I'd love to be able to do something like this:
class MyContainer
{
public:
mydatatype value(this);
myotherdatatype anothervalue(this);
...and so on...
};
This way, I could even use a preprocessor definition to simplify even
more the system to the user.
But, AFAIK, it's not possible to initialize members in the class
declaration like this, so the user would need to _both_ declare
members in the class declaration, and initialize them with the 'this'
pointer in the container class constructor... yes, it doesn't look
like a severe annoyance, but it's an important annoyance for the
system I'm designing, where I just wish that when the user declares a
registrable data member, it's automatically registered as a member of
the class without any further lines of code.
So, I now repeat the post question: what do you think it's the
shortest way of constructing array of members of a class?
TIA
ardi