Calling virtual override from base function fails

Discussion in 'C++' started by David, Jun 27, 2004.

  1. David

    David Guest

    Can anyone tell me why the following code crashes? The pure base
    class StaticObjectRegistry stores pointers to derived classes upon
    construction. Later it uses these pointers to call the overridden
    function ClearStaticData() through its base function Handler(). That
    is where the code crashes. (I'm using VC++6.0)


    #include <iostream>
    #include <list>

    using namespace std;

    class StaticObjectRegistry
    {
    public:
    static list<StaticObjectRegistry*> objects;
    StaticObjectRegistry() {
    static bool registered;
    if(!registered) {
    objects.push_back(this);
    registered = true;
    }
    };
    static void ResetStaticObjects(void);
    void Handler(void) {
    ClearStaticData();
    }
    virtual void ClearStaticData(void) = 0;
    };

    list<StaticObjectRegistry*> StaticObjectRegistry::eek:bjects;

    void StaticObjectRegistry::ResetStaticObjects(void) {
    for(list<StaticObjectRegistry*>::iterator i = objects.begin(); i !=
    objects.end(); i++)
    (*i)->Handler();
    }

    class P : public StaticObjectRegistry {
    public:
    void ClearStaticData(void) {cout << "In p.clear" << endl; c = 0;}
    static int c;
    P() {c++; cout << "P: " << c << endl;};
    };

    int P::c;

    void main()
    {
    P p;
    p.ResetStaticObjects();
    }
     
    David, Jun 27, 2004
    #1
    1. Advertising

  2. David

    jones Guest

    David wrote:
    > Can anyone tell me why the following code crashes? The pure base
    > class StaticObjectRegistry stores pointers to derived classes upon
    > construction. Later it uses these pointers to call the overridden
    > function ClearStaticData() through its base function Handler(). That
    > is where the code crashes. (I'm using VC++6.0)


    What do you mean "it crashes" ? does it segfault? i've tried it with gcc
    3.3.2 and it works perfectly, of course making "main" return int and
    not void.
     
    jones, Jun 27, 2004
    #2
    1. Advertising

  3. David

    David Guest

    Oops ... looks like I oversimplified. To get the crash add the
    following code:

    void Sub(void) {P test;}

    void main()
    {
    Sub();
    P p;
    p.ResetStaticObjects();
    }

    .... which makes it clear that I am trying to do something a little
    more difficult. Here, the "test" instance is registered and then
    destroyed but the StaticObjectRegistry doesn't remove the pointer to
    it, and doesn't add a pointer to the new instance in main(). But
    correctly creating and destroying pointers to every object instance
    doesn't meet my goals.


    What I wanted was to be able to reset static data on multiple classes
    EVEN IF THERE ARE NO CURRENT INSTANCES. Here's a version that
    accomplishes this. I guess I'm answering my own question here, but
    maybe others will find this StaticObjectRegistry to be useful:

    #include <algorithm>
    #include <iostream>
    #include <list>

    using namespace std;

    class StaticObjectRegistry
    {
    public:
    typedef void (*ResetFunction)();
    static void ResetStaticObjects(void);

    StaticObjectRegistry(ResetFunction RF) {
    if(find(ResetObjects.begin(), ResetObjects.end(), RF) ==
    ResetObjects.end())
    ResetObjects.push_back(RF);
    };
    private:
    StaticObjectRegistry();
    static list<ResetFunction> ResetObjects;
    };

    void StaticObjectRegistry::ResetStaticObjects(void) {
    for(list<ResetFunction>::iterator i = ResetObjects.begin(); i !=
    ResetObjects.end(); i++)
    (**i)();
    }

    list<StaticObjectRegistry::ResetFunction>
    StaticObjectRegistry::ResetObjects;

    // BEGIN SAMPLE USAGE
    class P : public StaticObjectRegistry {
    public:
    static void ClearStaticData(void) {cout << "In p.clear" << endl;
    c = 0;};
    static int c;
    P() : StaticObjectRegistry(ClearStaticData) {c++; cout << "P: "
    << c << endl;};
    };

    int P::c;

    void Sub(void) {
    P test;
    }

    void main()
    {
    Sub();
    P::ResetStaticObjects();
    P p;
    p.ResetStaticObjects();
    }
     
    David, Jun 27, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page