I am not exactly new to C++ but I'm not that proficient either, and am brand new to this board. I am trying to write my own Number class, such as the one featured in the Java API, to use as part of a parsing calculator I am writing. I'll post my code and then ask my question.
-----------------------------------------------------
My question: You can see where I have marked the destructors above as being
error prone. I have tested the program with blank destructors and it works fine
but I am concerned with memory leaks if I am going to use this in a real program. When I include the destructors as pictured above, it crashes with a bad stack error at runtime, as if maybe the data members are being deleting out of the proper order? Did I mention I don't have much experience with inheritance except a little in Java?
Can you not delete the memory allocated by these inherited void pointers?
When I compile with a destructor in the base class that deletes the void pointer from there, it gives a warning that deleting a void pointer yields undefined behavior. So I'm unsure what to do. Am I just trying to go about this the wrong way? Any help would be appreciated.
You're trying to delete a pointer to an automatically (I think. Either that or statically) allocated int.
I could write an equivalent of what you're trying to do:
1 2 3 4 5
struct A{
int a;
A(){a=0;}
~A(){delete &a;} //runtime error
};
To keep your design, you could do (I've lost all naming patience for the day):
Thanks... I realized sometime after I posted the obvious reason why delete
doesn't work, because it wasn't allocated with new. I don't quite have a grasp on memory management yet, but I'm learning. I need a good thorough guide on it. I made the code work a different way. I'm just avoiding any implementation in Number whatsoever and using a dynamic_cast to Integer and Double.