473,395 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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();
}
Jul 22 '05 #1
2 1985
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.

Jul 22 '05 #2
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();
}
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
6
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
by: Mark | last post by:
Hi, I have an abstract class which I want my other classes to inherit from. In the constructor of the abstract class I want to check if certain virtual functions have been overloaded or not. Is...
10
by: asm23 | last post by:
Hi, I'm using Intel C++ compiler 9 to compiler a project. But, there are a lot of warning saying like "virtual function override intended....". I searched old messages in Google groups, someone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.