Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling virtual override from base function fails

Reply
Thread Tools

Calling virtual override from base function fails

 
 
David
Guest
Posts: n/a
 
      06-27-2004
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: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();
}
 
Reply With Quote
 
 
 
 
jones
Guest
Posts: n/a
 
      06-27-2004
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.

 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      06-27-2004
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();
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why polymorph fails when virtual function is decleared private in base class and public in derived class? Fred C++ 2 06-03-2007 02:59 PM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM



Advertisments