#include <stdlib.h
#include <string.h
#include <stdio.h
#include <assert.h
#define zeroclass(PTR,TYPE) \
{ \
int Error_Different_Types[ sizeof(*(PTR)) == sizeof(TYPE) ]={0}; \
struct zeroclass_bigger_ : public TYPE { virtual zeroclass_muchbigger(){} }; \
if( sizeof(zeroclass_bigger_) sizeof(TYPE) ) memset( (PTR), 0, sizeof(TYPE) );\
else memset( (((void**)(PTR))+1), 0, sizeof(TYPE)-sizeof(void*) ); \
}
struct pepe
{
int Count;
pepe::pepe( void )
{
zeroclass( this, pepe );
}
void Alive( void )
{
printf( "(Normal)I am alive[%d]!!!\n", Count);
}
};
struct pepe2
{
int Count;
pepe2::pepe2( void )
{
zeroclass( this, pepe2 );
}
virtual void Alive( void )
{
printf( "(Virtual)I am alive[%d]!!!\n", Count);
}
};
void main( void )
{
pepe P1;
pepe2 P2;
P1.Alive();
P2.Alive();
// Also can be use like this
zeroclass( &P1, pepe );
zeroclass( &P2, pepe2 );
P1.Alive();
P2.Alive();
} |