The local variables and local functions are inside class body. You
define a variable to the class "Reg reg;" in the main function. The reg
variable has a pointer. The pointer gives memory address. The memory
address accesses local variable and local function. It is fine design
according to your preferance.
I try to remove a pointer so I let local variable and local function as
global to
access to the memory directly without needing a pointer. I put static
keyword to the local function inside class.
Why can't I put static on func1(), func2(), func3(), and func4()
functions?
Here is an example of x86 instruction and take a look of my source code
below.
00411CEE call Reg::Init (4111A9h) /* Init() function has static -- one x86
instruction */
Reduced x86 instruction helps to increase critical performance in a
loop.
0041328E lea ecx,[reg] /* Init() function has non-static member in class
Reg -- two x86 instructions */
00413291 call Reg::Init (4111FEh)
According to my speed test, extra x86 instruction for the pointer is
slower than one instruction to do direct memory.
/* main.cpp */
class Reg
{
private:
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char funcCycle;
void func1(void);
void func2(void);
void func3(void);
void func4(void);
typedef void (Reg::*pfunc)();
static const pfunc pf[4];
public:
static void Init (void);
static void Run(void);
} _reg;
const Reg::pfunc Reg::pf[4] = { &Reg::func1, &Reg::func2, &Reg::func3,
&Reg::func4 };
void Reg::Init (void)
{
_reg.funcCycle = 0;
}
void Reg::func1(void)
{
_reg.a += 2;
_reg.b += 4;
_reg.c += 8;
}
void Reg::func2(void)
{
_reg.a -= 2;
_reg.b -= 4;
_reg.c -= 8;
}
void Reg::func3(void)
{
_reg.a *= 2;
_reg.b *= 4;
_reg.c *= 8;
}
void Reg::func4(void)
{
_reg.a /= 2;
_reg.b /= 4;
_reg.c /= 8;
}
void Reg::Run(void)
{
(_reg.*pf[_reg.funcCycle])();
_reg.funcCycle++;
_reg.funcCycle &= 3;
}
int main()
{
_reg.Init();
int count = 0;
while (count++ < 10)
_reg.Run();
return 0;
}
--
Yours Truly,
Bryan Parkoff