Sign in to follow this  
TrueTom

[C++] List of derived classes

Recommended Posts

Hi, consider following code:

class AAA
{
}

class BBB : public AAA
{
    static int i;

    static void DoSomethingWithStatic();
}

class CCC : public AAA
{
    static double i;

    static void DoSomethingWithStatic();
}

I have several instances of BBB and CCC and need to call DoSomethingWithStatic once for every type (BBB, CCC) (more than once wouldn't hurt but is unneccessary because of static). I have several ideas how to achieve this during runtime but on the other hand everything is know at compile time but I don't think it is possible to implement it. I tried to create a list of function pointers but this doesn't work without a "static constructor" (a function which is called once for every class type) (ok, i have the idea of static class variable in the constructor, but i haven't tried it yet). Anyone who could see this from a different pov, i'm a bit stuck at the moment... :) Regards, Thomas

Share this post


Link to post
Share on other sites
If you only need the static function called when a instance of that type actually exists, then you could try calling it in the constructor. If calling it more than once doesn't hurt then this will work fine. But if you want more control over when these functions are called, i.e only once an instance of each type exists rather than an instance of only a single type, then you'll probably have to call the function manually for each type in a separate function, possible a global function or a static function of AAA if you prefer.

I want to help you more but I don't really have too many details with regards to what you're trying to achieve [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by TrueTom
I tried to create a list of function pointers but this doesn't work without a "static constructor"


All you have to do is create a type for each child whose constructor calls the function you need. Have a static instance of that type as a part of the class.

This can easily be implemented with CRTP:

class AAA
{
// Blah
};

template< typename YourType >
class base
{
struct constructor
{
constructor() { YourType::DoSomethingWithStatic(); }
};

static constructor dummy;
};

template< typename YourType >
typename base< YourType >::constructor base< YourType >::dummy;

class BBB
: public AAA
, base< BBB >
{
// blah blah
static void DoSomethingWithStatic();
};

class CCC
: public AAA
, base< CCC >
{
// blah blah
static void DoSomethingWithStatic();
};



That will make it so every type which inherits from an instantiation of the base template has their "DoSomethingWithStatic" function called at runtime.

If you aren't guaranteed that the function is the same name in each class, you can pass the function instead of the type to the template (with corresponding changes made to the base template).

Edit: one thing to remember about this is that since it's based off of the construction of globals, you are only guaranteed that they will automatically be called before main is executed. If you have functions which run before main is executed (IE constructors of other objects that are globals) that rely on those functions being called prior to their execution, you have no way of guaranteeing that this will be the case. Instead, if you absolutely must account for that situation, you'd have to make explicit calls to those functions.

Share this post


Link to post
Share on other sites
Well, I have the above class design and I need to modify the static members of the derived classes somehow.

The best idea I have is to create an global array (AAA *) and add one AAA which is an BBB, add an AAA which is an CCC, and so on...

The problem with this is, I have to do it in one central file... :-|

Share this post


Link to post
Share on other sites
Quote:
Original post by TrueTom
Well, I have the above class design and I need to modify the static members of the derived classes somehow.

I'm not seeing why my solution doesn't solve that problem. The functions are still in the child classes and are capable of modifying corresponding child's static data.

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
Quote:
Original post by TrueTom
Well, I have the above class design and I need to modify the static members of the derived classes somehow.

I'm not seeing why my solution doesn't solve that problem. The functions are still in the child classes and are capable of modifying corresponding child's static data.


Sorry, I replied befor seeing your answer. I was ment for Zipster.

Your solution works fine (and in a way I never had thought of ;) ).

But it goes in a slightly wrong direction (my fault, didn't describe my problem to well). But I try again:


I have several instances of several classes. Every class shares data between every instance with 'static'.

Now I need to write a function which tells every class to init the shared data (for example).

My idea was to derive every class from a base class, in this base class i write a virtual function which every child overwrites.

Now I can save every instance in a vector and call for every instance 'virtual BaseClass->InitYourData()'.

Problem is: Since the data is static I only need to call once per derived class, every other call would be unnessecary.

Other idea was to use a static function in every derived class and save a function pointer to it in a global array. Problem is: How to get the function pointer into the array at compile time?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this