473,513 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Write Function Pointer Array

I would like to design an object using class. How can this class
contain 10 member functions. Put 10 member functions into member
function pointer array.
One member function uses switch to call 10 member functions. Can
switch be replaced to member function pointer array?
Please provide me an example of source code to show smart pointer
inside class. Thanks....
Jun 28 '08 #1
5 3618
On Jun 28, 3:07 am, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
I would like to design an object using class. How can this class
contain 10 member functions. Put 10 member functions into member
function pointer array.
One member function uses switch to call 10 member functions. Can
switch be replaced to member function pointer array?
Yes, but why? What controls the choice? On the whole member
function pointers are awkward and complex; a number of otherwise
competent C++ programmers seem to have problems with them,
because of the syntax. I use them from time to time, but my
experience has been that as the class evolves, they end up being
replaced by "agents", small stand-alone objects which call the
function (and possibly contain additional data).

Anyway, supposing the calling function receives an int:

void
MyClass::dispatch( int key )
{
static void (MyClass::* const table[])() =
{
&MyClass::f1,
&MyClass::f2,
// ...
} ;
assert( key >= 0 && key < size( table ) ) ;
(this->*table[ key ])() ;
}

Please provide me an example of source code to show smart
pointer inside class. Thanks....
You mean something like:

class MyClass
{
private:
std::auto_ptr< Something myPtr ;
} ;

? (Replace std::auto_ptr with smart pointer of choice.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 28 '08 #2
On Jun 28, 2:23*am, James Kanze <james.ka...@gmail.comwrote:
On Jun 28, 3:07 am, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
* * *I would like to design an object using class. *How can this class
contain 10 member functions. *Put 10 member functions into member
function pointer array.
* * *One member function uses switch to call 10 member functions.*Can
switch be replaced to member function pointer array?

Yes, but why? *What controls the choice? *On the whole member
function pointers are awkward and complex; a number of otherwise
competent C++ programmers seem to have problems with them,
because of the syntax. *I use them from time to time, but my
experience has been that as the class evolves, they end up being
replaced by "agents", small stand-alone objects which call the
function (and possibly contain additional data).
Thank you very much for your assistance. Yes, you do answer my
question right. Can you please describe what you mean "agents"?

Member Function Pointer Array is ideal for CPU's Opcode emulator like
dispatch() or fetch().
Anyway, supposing the calling function receives an int:

* * void
* * MyClass::dispatch( int key )
* * {
* * * * static void (MyClass::* const table[])() =
* * * * {
* * * * * * &MyClass::f1,
* * * * * * &MyClass::f2,
* * * * * * // ...
* * * * } ;
* * * * assert( key >= 0 && key < size( table ) ) ;
* * * * (this->*table[ key ])() ;
* * }
dispatch() function as above makes sense. Many C programmers tell
that C++ class is slower. They want to stick global variable and
global functions for performance reason.

According to my research, global functions have *direct* memory. The
member functions have *indirect* memory. It means that member
function must receive memory address from *this pointer* before member
function can be called.

C++ programmers wants to convert global variable and global function
into C++ class. They say that C++ class is about 25% slower
performance than C style source code because extra instruction needs
to access indirection.

It is the time to start designing CPU's opcode emulator on C++ class.
They expect faster Intel / AMD CPUs or other machines with the speed
of 4 GHz and beyond.

Desinging class is a lot of easier to reduce debugging time and less
prone error.

Do you agree to continue using C++ style language today?
Please provide me an example of source code to show smart
pointer inside class. *Thanks....

You mean something like:

* * class MyClass
* * {
* * private:
* * * * std::auto_ptr< Something myPtr ;
* * } ;
No. it does not what I mean. I thought that smart pointer is meant to
be pointer table of member function arrays. You can drop this issue.
Never mind...
James Kanze (GABI Software) * * * * * *
I am curious what do you call "GABI Software"? Anyway...Thanks...
Jun 28 '08 #3
On Jun 28, 6:55 pm, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
On Jun 28, 2:23 am, James Kanze <james.ka...@gmail.comwrote:
On Jun 28, 3:07 am, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
I would like to design an object using class. How can this class
contain 10 member functions. Put 10 member functions into member
function pointer array.
One member function uses switch to call 10 member functions. Can
switch be replaced to member function pointer array?
Yes, but why? What controls the choice? On the whole member
function pointers are awkward and complex; a number of otherwise
competent C++ programmers seem to have problems with them,
because of the syntax. I use them from time to time, but my
experience has been that as the class evolves, they end up being
replaced by "agents", small stand-alone objects which call the
function (and possibly contain additional data).
Thank you very much for your assistance. Yes, you do answer
my question right. Can you please describe what you mean
"agents"?
Small stand-alone objects, which (probably) call the funnction
actually desired. The advantage of this, as opposed to pointers
to member functions, is that the objects can contain additional
data. Thus, you can add cases where two keys call the same
function, but with different arguments.

Agents will generally all derive from a common abstract base
class, with a single function which performs the desired
operation. The array will be of pointers to this base class.
It requires a little bit more typing to set up, but it's a lot
more flexible, and for most C++ programmers that I've
encountered, a lot more readable.
Member Function Pointer Array is ideal for CPU's Opcode
emulator like dispatch() or fetch().
Anyway, supposing the calling function receives an int:
void
MyClass::dispatch( int key )
{
static void (MyClass::* const table[])() =
{
&MyClass::f1,
&MyClass::f2,
// ...
} ;
assert( key >= 0 && key < size( table ) ) ;
(this->*table[ key ])() ;
}
dispatch() function as above makes sense. Many C programmers
tell that C++ class is slower. They want to stick global
variable and global functions for performance reason.
According to my research, global functions have *direct*
memory. The member functions have *indirect* memory. It
means that member function must receive memory address from
*this pointer* before member function can be called.

C++ programmers wants to convert global variable and global
function into C++ class. They say that C++ class is about 25%
slower performance than C style source code because extra
instruction needs to access indirection.
It is the time to start designing CPU's opcode emulator on C++
class. They expect faster Intel / AMD CPUs or other machines
with the speed of 4 GHz and beyond.
I'm not really sure I understand. An emulator, today, doesn't
emulate single instructions; it emulates blocks of code. And
its speed is determined by the size of the blocks, and how
effectively it can deduce the actual semantics of the block, and
convert that into native code.
Desinging class is a lot of easier to reduce debugging time
and less prone error.
Do you agree to continue using C++ style language today?
I'm in favor of using the most effective tool to do the job.
For large projects, C++ is certainly one of the leading
candidates---another possibility might include Ada 95. I can't
imagine any situation where one would select C, however.

[...]
James Kanze (GABI Software)
I am curious what do you call "GABI Software"?
It's the name I trade under. (I'm a freelance professional,
providing services on a contract basis.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 28 '08 #4
On Jun 28, 1:39*pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 28, 6:55 pm, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:


On Jun 28, 2:23 am, James Kanze <james.ka...@gmail.comwrote:
On Jun 28, 3:07 am, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
* * *I would like to design an object using class. *How canthis class
contain 10 member functions. *Put 10 member functions into member
function pointer array.
* * *One member function uses switch to call 10 member functions. *Can
switch be replaced to member function pointer array?
Yes, but why? *What controls the choice? *On the whole member
function pointers are awkward and complex; a number of otherwise
competent C++ programmers seem to have problems with them,
because of the syntax. *I use them from time to time, but my
experience has been that as the class evolves, they end up being
replaced by "agents", small stand-alone objects which call the
function (and possibly contain additional data).
Thank you very much for your assistance. *Yes, you do answer
my question right. *Can you please describe what you mean
"agents"?

Small stand-alone objects, which (probably) call the funnction
actually desired. *The advantage of this, as opposed to pointers
to member functions, is that the objects can contain additional
data. *Thus, you can add cases where two keys call the same
function, but with different arguments.

Agents will generally all derive from a common abstract base
class, with a single function which performs the desired
operation. *The array will be of pointers to this base class.
It requires a little bit more typing to set up, but it's a lot
more flexible, and for most C++ programmers that I've
encountered, a lot more readable.
If you want to use small stand-alone object without using class, you
can only create one object at this time. The flexibaility is less on
C style language.

Class is ideal to show multiple objects. You can use class to define
more than one object. Think of class CPU { public: }. Create an
array of four CPU objects. You can expect four CPU objects to run at
the same time like four emulators are running.
Member Function Pointer Array is ideal for CPU's Opcode
emulator like dispatch() or fetch().
Anyway, supposing the calling function receives an int:
* * void
* * MyClass::dispatch( int key )
* * {
* * * * static void (MyClass::* const table[])() =
* * * * {
* * * * * * &MyClass::f1,
* * * * * * &MyClass::f2,
* * * * * * // ...
* * * * } ;
* * * * assert( key >= 0 && key < size( table ) ) ;
* * * * (this->*table[ key ])() ;
* * }
dispatch() function as above makes sense. *Many C programmers
tell that C++ class is slower. *They want to stick global
variable and global functions for performance reason.
According to my research, global functions have *direct*
memory. *The member functions have *indirect* memory. *It
means that member function must receive memory address from
*this pointer* before member function can be called.
C++ programmers wants to convert global variable and global
function into C++ class. *They say that C++ class is about 25%
slower performance than C style source code because extra
instruction needs to access indirection.
It is the time to start designing CPU's opcode emulator on C++
class. *They expect faster Intel / AMD CPUs or other machines
with the speed of 4 GHz and beyond.

I'm not really sure I understand. *An emulator, today, doesn't
emulate single instructions; it emulates blocks of code. *And
its speed is determined by the size of the blocks, and how
effectively it can deduce the actual semantics of the block, and
convert that into native code.
I am sorry that you misunderstand. I do not talk about emulator. I
talk about indirect memory and direct memory. C++ Compiler translates
class and *this pointer* into native x86 machine language. You can
take a look at disassembler. You will see *lea instruction* and *mov
instruction*.

Global variables and global functions use *direct memory* so you can
see *mov instruction*. Member variables and member functions inside
class use *this pointer*. The class needs to read *this pointer*
before it can access member variable or member function. *lea
instruction* is the *this pointer* and *mov instruction* is the member
variable or member function.

I recall that class is 25% performance slower than C style language
because it has extra instruction to be fetched like lea instruction.

Jun 28 '08 #5
On Jun 29, 1:20 am, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
On Jun 28, 1:39 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 28, 6:55 pm, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
On Jun 28, 2:23 am, James Kanze <james.ka...@gmail.comwrote:
On Jun 28, 3:07 am, Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
I would like to design an object using class. How can this class
contain 10 member functions. Put 10 member functions into member
function pointer array.
One member function uses switch to call 10 member functions.Can
switch be replaced to member function pointer array?
Yes, but why? What controls the choice? On the whole member
function pointers are awkward and complex; a number of otherwise
competent C++ programmers seem to have problems with them,
because of the syntax. I use them from time to time, but my
experience has been that as the class evolves, they end up being
replaced by "agents", small stand-alone objects which call the
function (and possibly contain additional data).
Thank you very much for your assistance. Yes, you do answer
my question right. Can you please describe what you mean
"agents"?
Small stand-alone objects, which (probably) call the funnction
actually desired. The advantage of this, as opposed to pointers
to member functions, is that the objects can contain additional
data. Thus, you can add cases where two keys call the same
function, but with different arguments.
Agents will generally all derive from a common abstract base
class, with a single function which performs the desired
operation. The array will be of pointers to this base class.
It requires a little bit more typing to set up, but it's a lot
more flexible, and for most C++ programmers that I've
encountered, a lot more readable.
If you want to use small stand-alone object without using
class, you can only create one object at this time. The
flexibaility is less on C style language.
Class is ideal to show multiple objects. You can use class to
define more than one object. Think of class CPU { public: }.
Create an array of four CPU objects. You can expect four CPU
objects to run at the same time like four emulators are
running.
If you have an array, you have multiple objects. All we're
talking about is the type of the object.

Of course, with agents, you do introduce an additional level of
indirection; the array contains pointers to objects, and not the
objects themselves, since we need pointers for polymorphism to
work. Which is the additional typing I referred to. It pays
off in added flexibility, however---and to top it off, it's
usually a bit faster.
Member Function Pointer Array is ideal for CPU's Opcode
emulator like dispatch() or fetch().
Anyway, supposing the calling function receives an int:
void
MyClass::dispatch( int key )
{
static void (MyClass::* const table[])() =
{
&MyClass::f1,
&MyClass::f2,
// ...
} ;
assert( key >= 0 && key < size( table ) ) ;
(this->*table[ key ])() ;
}
dispatch() function as above makes sense. Many C programmers
tell that C++ class is slower. They want to stick global
variable and global functions for performance reason.
According to my research, global functions have *direct*
memory. The member functions have *indirect* memory. It
means that member function must receive memory address from
*this pointer* before member function can be called.
C++ programmers wants to convert global variable and
global function into C++ class. They say that C++ class
is about 25% slower performance than C style source code
because extra instruction needs to access indirection. It
is the time to start designing CPU's opcode emulator on
C++ class. They expect faster Intel / AMD CPUs or other
machines with the speed of 4 GHz and beyond.
I'm not really sure I understand. An emulator, today,
doesn't emulate single instructions; it emulates blocks of
code. And its speed is determined by the size of the
blocks, and how effectively it can deduce the actual
semantics of the block, and convert that into native code.
I am sorry that you misunderstand. I do not talk about
emulator. I talk about indirect memory and direct memory.
C++ Compiler translates class and *this pointer* into native
x86 machine language. You can take a look at disassembler.
You will see *lea instruction* and *mov instruction*.
Global variables and global functions use *direct memory* so
you can see *mov instruction*. Member variables and member
functions inside class use *this pointer*. The class needs to
read *this pointer* before it can access member variable or
member function.
With any decent compiler, the this pointer will be in memory,
and on most machines, a based access will be at least as fast,
if not faster, than a direct access. At least on my Sun Sparc,
accessing member variables is faster than accessing global
variables. (The Intel architecture is a bit hindered here by a
lack of registers, so most compilers will only put the this
pointer into a register when optimization is turned on. On the
other hand, no one is going to be using the Intel architecture
if performance is an issue.)
*lea instruction* is the *this pointer* and *mov instruction*
is the member variable or member function.
I recall that class is 25% performance slower than C style
language because it has extra instruction to be fetched like
lea instruction.
You recall wrong. If two programs do the same thing, and
performance is an issue, C++ always wins (compared to C, at
least), because of better encapsulation.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 29 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3040
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought...
58
10050
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
4
3422
by: m sergei | last post by:
main(int argc, char *argv) the second parameter, argv, to function main takes a pointer to an array. can we also write it in terms of a reference rather than a pointer ? (like &..)
13
409
by: Timothy Madden | last post by:
Hello all I'm facing a very strange problem. I need to define a function that takes as argument something like a pointer to itself. It takes as argument a pointer of the same type as a pointer to itself and with possibly other pointer-to-function value. But I don't have a data type for such a pointer because the function prototype is in the...
8
29017
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If possible how could one pass these seven values in the array to a function that would check the values. I tried return y but it didn't work
11
2070
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway... My implementation seems to work great for primitive types, structures and unions, but I can't quite get an array of function-pointers working...
27
2443
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to...
9
2177
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a simple array, to pass to a range constructor for a const vector. Here is some demonstration code: #include <iostream> using namespace std;
4
4377
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function pointer array instead of switch. They believe that ordinary function pointer is much faster than switch. Think of one variable called...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.