Shortest way of constructing array of members of a class?


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
 
Ad

Advertisements

V

Victor Bazarov

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.

Yes, and if the user forgets to check a pointer for being null before
dereferencing ot or to check a number for being zero before dividing by
it... You can't prevent errors. The more "functionality" your system
will try to provide, the more inefficient and bulky it becomes.

Document the use of your system and clearly state the requirements for
the classes that are supposed to be included into the "limited RTTI
functionality".
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.

.... but that thing just doesn't exist, does it?

There are probably ways to enforce the use of your registering system in
the class constructor. For instance, you could create the registering
class policy (which does the registering during its own construction)
and tell the future class implementers to inherit from your class. Your
class would have no default constructor, so the users will need to
initialize it in their class' initializer list. How they do it is up to
them, so make sure your documentation clearly states how this needs to
happen.

template<class T> class LimitedRTTIRegistrarByArdi
{
public:
LimitedRTTIRegistrarByArdi(int number ...);
};

....

class Foo : LimitedRTTIRegistrarByArdi<Foo>
{
...
int value;
Foo() : /* this has to be here: */
LimitedRTTIRegistrarByArdi<Foo>(1, &value) {}
};

There are obvious drawbacks of that as well, you need to process the
ellipsis. You can probably come up with a fancier way to use tuples or
initializer lists.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top