B works, A does not. Is there any way to make A work without using new (that includes placement new)? The code is more or less identical and I'm sure my overloaded new/delete are being called. The only difference is that A doesn't call the constructor but that shouldn't matter since it's empty (?).
I'm guessing it has something to do with vtables but I'm not sure how to fix it (if at all possible).
A:
#include <stdio.h>
#include <stdlib.h>
struct ishazbot {
virtual void do_stuff(void) = 0;
};
class shazbot : public ishazbot
{
public:
shazbot() {}
~shazbot() {}
void do_stuff(void) { printf("SHAZBOT!\n"); }
};
int main(void)
{
ishazbot* sb = (ishazbot*)malloc(sizeof(shazbot));
sb->do_stuff();
free(sb);
return 0;
}
B:
#include <stdio.h>
#include <stdlib.h>
void* operator new(size_t size) { return malloc(size); }
void operator delete(void* p) { free(p); }
struct ishazbot {
virtual void do_stuff(void) = 0;
};
class shazbot : public ishazbot
{
public:
shazbot() {}
~shazbot() {}
void do_stuff(void) { printf("SHAZBOT!\n"); }
};
int main(void)
{
ishazbot* sb = (ishazbot*)new shazbot();
sb->do_stuff();
delete sb;
return 0;
}