19Mar/08Off
The Most Evil Code Ever
I have just created quite possibly the most evil code to mankind, its an abomination. However it did double my FPS.
Basically, I thought to myself "a class is basically just a structure, and its functions are basically the same function but with a pointer to the class at the beginning, so why can't I just have a pointer to a function within the class, and the class pointer and combine the to for function callbacks, but on a generic level, only have the base class structure known". And so thats what I set out to do, to test if it would work.
Now instead of calling an OnEvent function that is pure virtual in the base class, I can call the functions directly, skipping the middleman. It compiles on g++ and visual studio 9.
//
//
#include <iostream>
//
//
class IBase
{
public:
IBase()
{
}
virtual ~IBase()
{
}
int testno( const int j )
{
return 3;
}
};
//
//
class clsDerived : public IBase
{
public:
clsDerived()
:
IBase()
{
}
int test( const int i )
{
return i;
}
};
//
//
class clsMyDerive : public IBase
{
public:
clsMyDerive()
:
IBase()
{
}
virtual ~clsMyDerive()
{
}
virtual int tested( const int i )
{
return -i;
}
};
//
//
class clsMyD : public clsMyDerive
{
public:
clsMyD()
:
clsMyDerive()
{
}
int testy( const int i )
{
return ~i;
}
int tested( const int i )
{
return -1 + 1;
}
};
//
//
typedef int (IBase::*_FN_CLSGAME)( const int );
//
//
int main()
{
IBase * test = new clsDerived();
IBase * test2 = new clsMyDerive();
IBase * test3 = new clsMyD();
_FN_CLSGAME q;
q = reinterpret_cast<_FN_CLSGAME>( &clsDerived::test );
std::cout << "Test 1: %d" << (test->*q)(12) << std::endl;
q = reinterpret_cast<_FN_CLSGAME>( &clsMyDerive::tested );
std::cout << "Test 2: %d" << (test2->*q)(12) << std::endl;
q = reinterpret_cast<_FN_CLSGAME>( &clsMyDerive::tested );
std::cout << "Test 3: %d" << (test3->*q)(12) << std::endl;
return 0;
}


