 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Guest
|
Posted: Sun Jun 27, 2004 12:47 pm Post subject: Calling virtual override from base function fails |
|
|
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::objects;
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();
}
|
|
| Back to top |
|
 |
jones Guest
|
Posted: Sun Jun 27, 2004 1:26 pm Post subject: Re: Calling virtual override from base function fails |
|
|
David wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
David Guest
|
Posted: Sun Jun 27, 2004 7:15 pm Post subject: Re: Calling virtual override from base function fails |
|
|
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();
}
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|