-
February 15th, 2006, 04:56 AM
#1
Forward Declaration compilation warning (g++)
I have the following class structure for a (discrete event) simulation program:
Code:
class simulation;
class event
{
public
event(simulation* sim_pointer) {p_sim = sim_pointer;};
virtual ~event() {};
virtual void process() = 0;
protected:
simulation* p_sim;
};
class simulation
{
public:
simulation() {};
virtual ~simulation() {};
virtual void schedule(event* p_event);
//some more functions
protected:
list<event*> event_list;
};
class special_event;
class special_simulation : public simulation
{
special_simulation() : simulation() {};
virtual ~special_simulation() {if (p_save_event) delete p_save_event;};
//more functions
protected:
special_event* p_save_event;
//more class members
};
class special_event : public event
{
public
special_event(special_simulation* special_sim_pointer) : event(special_sim_pointer) {};
virtual ~special_event() {};
virtual void process() = 0;
protected:
//some class members
};
In this case I want to use the special_simulation class (inherited from the base simulation class). The simulation classes processes events derived from the pure virtual event class (including events inherited from special_event_class).
When compiling the code with g++ I get the following warnings:
Code:
In destructor 'virtual special_simulation::~special_simulation()':
warning: possible problem detected in invocation of delete operator:
warning: invalid use of undefined type `struct special_event'
warning: forward declaration of `struct special_event'
note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
Is this warning serious, what are possible implications (memory leaks?)?
How can I fix this problem?
-
February 15th, 2006, 05:14 AM
#2
Re: Forward Declaration compilation warning (g++)
Originally Posted by dus
Is this warning serious, what are possible implications (memory leaks?)?
It sounds serious and its never a good idea to ignore warnings.
How can I fix this problem?
Just move the definition of your destructor out of your class declaration, e.g. move the following to the end of the file:
Code:
inline
special_simulation::~special_simulation()
{
if (p_save_event) delete p_save_event;
};
Last edited by treuss; February 15th, 2006 at 05:15 AM.
Reason: Removed virtual keyword
-
February 15th, 2006, 05:19 AM
#3
Re: Forward Declaration compilation warning (g++)
It's basically giving you the warning because it can't "see" the class's destructor, so there's a possibility that you may be doing a polymorphic delete on a class without a virtual destructor. Yes, pay attention to it; yes, do as treuss suggests, but I don't think it's serious as long as you are convinced that the delete is OK.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 15th, 2006, 05:20 AM
#4
Re: Forward Declaration compilation warning (g++)
You need to split definition and declaration, preferrably into .h and .cpp files because in order to delete an object, you require to have the complete class declaration being declared ahead of the code. In addition, in the constructor of simulation and special_simulation class, you should intialize the p_sim and p_save_event pointer to 0. If not, your program may crash in the destructors because they are pointing to some invalid address. Beside, in both destructors, it is valid to delete a NULL pointer without checking statement.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
February 15th, 2006, 05:27 AM
#5
Re: Forward Declaration compilation warning (g++)
Thanks all!
Treuss solution removed the warning!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|