I
Immortal Nephi
I did read section [33] under C++ FAQs Lite. It talks about pointer
to member functions and functionoids. Someone claims that
functionoids runs faster than traditional pointer to function and
pointer to member functions, but another denies their claim. They say
functionoids can be slow because it requires high overheads through
three pointers. I do not have profiler in front of me because I use
Visual Studio C++ Express Edition. I would appreciate if someone
already tested functionoids and pointer to member functions. They can
tell which is faster.
If I am going to use functionoids instead of pointer to member
functions at compile-time, then all derived classes are unable to
share base class’ data members. The only option is to use static data
member, but I don’t want it.
I create stand alone class. Stand alone class contains data
members. The derived classes are able to access stand alone class
directly. Stand alone class is necessary because I create two
separate functionoid arrays.
Here is an example of my code below.
class R
{
public:
R() : a( 0 ), b( 0 ), c(0 ) {}
~R() {}
int a;
int b;
int c;
};
class Top
{
public:
Top() : data2( 0 ) {}
~Top() {}
virtual void Execute() = 0;
static int data1;
int data2;
};
int Top::data1 = 0;
class A : public Top
{
public:
A( R &_pR ) : pR( &_pR ) {}
~A() {}
virtual void Execute()
{
pR->a += 0x1;
pR->b += 0x2;
pR->c += 0x4;
data1 += 1;
data2 += 1;
}
R* const pR;
};
class B : public Top
{
public:
B( R &_pR ) : pR( &_pR ) {}
~B() {}
virtual void Execute()
{
pR->a += 0x10;
pR->b += 0x20;
pR->c += 0x40;
data1 += 2;
data2 += 2;
}
R* const pR;
};
int main() HINSTANCE h_Instance,
{
R r1;
R r2;
A aa1( r1 );
B bb1( r1 );
A aa2( r2 );
B bb2( r2 );
Top *pTop1[2] = { &aa1, &bb1 };
Top *pTop2[2] = { &aa2, &bb2 };
pTop1[0]->Execute();
pTop1[1]->Execute();
pTop2[0]->Execute();
pTop2[1]->Execute();
int a1 = r1.a;
int a2 = r1.b;
int a3 = r1.c;
int b1 = r2.a;
int b2 = r2.b;
int b3 = r2.c;
system("pause");
return 0;
}
to member functions and functionoids. Someone claims that
functionoids runs faster than traditional pointer to function and
pointer to member functions, but another denies their claim. They say
functionoids can be slow because it requires high overheads through
three pointers. I do not have profiler in front of me because I use
Visual Studio C++ Express Edition. I would appreciate if someone
already tested functionoids and pointer to member functions. They can
tell which is faster.
If I am going to use functionoids instead of pointer to member
functions at compile-time, then all derived classes are unable to
share base class’ data members. The only option is to use static data
member, but I don’t want it.
I create stand alone class. Stand alone class contains data
members. The derived classes are able to access stand alone class
directly. Stand alone class is necessary because I create two
separate functionoid arrays.
Here is an example of my code below.
class R
{
public:
R() : a( 0 ), b( 0 ), c(0 ) {}
~R() {}
int a;
int b;
int c;
};
class Top
{
public:
Top() : data2( 0 ) {}
~Top() {}
virtual void Execute() = 0;
static int data1;
int data2;
};
int Top::data1 = 0;
class A : public Top
{
public:
A( R &_pR ) : pR( &_pR ) {}
~A() {}
virtual void Execute()
{
pR->a += 0x1;
pR->b += 0x2;
pR->c += 0x4;
data1 += 1;
data2 += 1;
}
R* const pR;
};
class B : public Top
{
public:
B( R &_pR ) : pR( &_pR ) {}
~B() {}
virtual void Execute()
{
pR->a += 0x10;
pR->b += 0x20;
pR->c += 0x40;
data1 += 2;
data2 += 2;
}
R* const pR;
};
int main() HINSTANCE h_Instance,
{
R r1;
R r2;
A aa1( r1 );
B bb1( r1 );
A aa2( r2 );
B bb2( r2 );
Top *pTop1[2] = { &aa1, &bb1 };
Top *pTop2[2] = { &aa2, &bb2 };
pTop1[0]->Execute();
pTop1[1]->Execute();
pTop2[0]->Execute();
pTop2[1]->Execute();
int a1 = r1.a;
int a2 = r1.b;
int a3 = r1.c;
int b1 = r2.a;
int b2 = r2.b;
int b3 = r2.c;
system("pause");
return 0;
}