T
Thomas Matthews
Hi,
How do I create a const table of pointers to member functions?
I'm implementing a Factory pattern (or jump table). I want to
iterate through the table, calling each member function until
a non-zero index is returned. Below is my attempt, which
generates compiler errors:
namespace Reference
{
class Base
{
};
}
class My_Class
{
public:
typedef int (My_Class:: * P_ADD_REC_FUNC)
(const Reference::Base& ref);
int add_reference(const Reference::Base& ref);
private:
int add_book(const Reference::Base& ref);
int add_magazine(const Reference::Base& ref);
const P_ADD_REC_FUNC add_func_table[];
};
//[1] The following definition generates the errors.
const P_ADD_REC_FUNC My_Class::add_func_table[] =
{
add_book, add_magazine
};
static const unsigned int NUM_ADD_FUNCS =
sizeof(add_func_table) / sizeof(add_func_table[0]);
int
My_Class ::
add_book(const Reference::Base& ref)
{
// Stubbed for now.
return 0;
}
int
My_Class ::
add_magazine(const Reference::Base& ref)
{
// Stubbed for now.
return 1;
}
int
My_Class ::
add_reference(const Reference::Base& ref)
{
int id(0);
for (unsigned int i = 0;
(id == 0) && (i < NUM_ADD_FUNCS);
++i)
{
id = add_func_table(ref);
}
return id;
}
The error that I am getting is:
Multiple declaration for "My_Class::add_func_table"
In the past I would use a table of pointers to
static member functions. This time I thought I
would change content of the table to use pointers
to {non-static} member functions since I am referring
to the table within the same class.
So, how should I be declaring a constant table of
pointers to member functions?
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
How do I create a const table of pointers to member functions?
I'm implementing a Factory pattern (or jump table). I want to
iterate through the table, calling each member function until
a non-zero index is returned. Below is my attempt, which
generates compiler errors:
namespace Reference
{
class Base
{
};
}
class My_Class
{
public:
typedef int (My_Class:: * P_ADD_REC_FUNC)
(const Reference::Base& ref);
int add_reference(const Reference::Base& ref);
private:
int add_book(const Reference::Base& ref);
int add_magazine(const Reference::Base& ref);
const P_ADD_REC_FUNC add_func_table[];
};
//[1] The following definition generates the errors.
const P_ADD_REC_FUNC My_Class::add_func_table[] =
{
add_book, add_magazine
};
static const unsigned int NUM_ADD_FUNCS =
sizeof(add_func_table) / sizeof(add_func_table[0]);
int
My_Class ::
add_book(const Reference::Base& ref)
{
// Stubbed for now.
return 0;
}
int
My_Class ::
add_magazine(const Reference::Base& ref)
{
// Stubbed for now.
return 1;
}
int
My_Class ::
add_reference(const Reference::Base& ref)
{
int id(0);
for (unsigned int i = 0;
(id == 0) && (i < NUM_ADD_FUNCS);
++i)
{
id = add_func_table(ref);
}
return id;
}
The error that I am getting is:
Multiple declaration for "My_Class::add_func_table"
In the past I would use a table of pointers to
static member functions. This time I thought I
would change content of the table to use pointers
to {non-static} member functions since I am referring
to the table within the same class.
So, how should I be declaring a constant table of
pointers to member functions?
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library