Re: Write Function Pointer Array
On Jun 28, 3:07 am, Immortal Nephi <Immortal_Ne...@satx.rr.com> wrote:
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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34