Deleting pointers to inherited data members in derived classes

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.

#ifndef NUMBER_H
#define NUMBER_H

class Number
{
protected:
void *data;

public:
Number() {}
virtual ~Number() {}
virtual void *getData() =0;
};


class Integer : public Number
{
private:
int value;

public:
Integer() { value = 0; data = &value; }
Integer( int i ) { value = i; data = &value; }

~Integer() { delete (int*)this->data; } //error here!!!!!!!

void *getData() { return this->data; }
};


class Double : public Number
{
private:
double value;

public:
Double() { value = 0.0; data = &value; }
Double( double d ) { value = d; data = &value; }

~Double() { delete (double*)this->data; } //error here!!!!!!

void *getData() { return this->data; }
};


#endif

...

-NumberMain.cpp-

#include <iostream>
#include "Number.h"

using std::cout;

int main()
{
Number *numptr;
int *intptr;
double *doubleptr;

Double d1(3.2);
Integer i1( 5 );


numptr = &i1;

if( typeid( *numptr ) == typeid( Double ) )
{
doubleptr = (double*)numptr->getData();
cout << "double value: " << *doubleptr << endl;
}
else
{
intptr = (int*)numptr->getData();
cout << "int value: " << *intptr << endl;
}

return 0;
}

-----------------------------------------------------
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):
1
2
3
4
struct B:A{
    A(){this->data=(void *)new int;}
    ~A(){delete (int *)this->data;}
}
Last edited on
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.
Topic archived. No new replies allowed.