Re: Creating table of const ptr to member funcs

From: Thomas Matthews (Thomas_MatthewsHatesSpam_at_sbcglobal.net)
Date: 11/03/03


Date: Mon, 03 Nov 2003 14:55:58 GMT

Rob Williscroft wrote:

> Thomas Matthews wrote in news:3FA56BEB.8060907@sbcglobal.net:
>
>
>>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?
>>
>>
>
>
> See comments inline.
>
> namespace Reference
> {
> class Base
> {
> };
> }
>
> class My_Class
> {
> public:
> typedef
> int (My_Class:: * const P_ADD_REC_FUNC_CONST)
> (Reference::Base const& ref)
> ;
> int add_reference(const Reference::Base& ref);
>
> private:
> int add_book(Reference::Base const& ref);
> int add_magazine(Reference::Base const & ref);
>
> // Note static and new typedef
> static P_ADD_REC_FUNC_CONST add_func_table[];
>
> public:
> static const unsigned int NUM_ADD_FUNCS;
> };
>
> //addition of My_Class::
> My_Class::P_ADD_REC_FUNC_CONST My_Class::add_func_table[] =
> {
> &My_Class::add_book, &My_Class::add_magazine
> };
>
> //Now a member
> unsigned int const My_Class::NUM_ADD_FUNCS =
> sizeof(My_Class::add_func_table)
> /
> sizeof(My_Class::add_func_table[0])
> ;
>
> int
> My_Class ::
> add_book(const Reference::Base& ref)
> {
> return 0;
> }
>
> int
> My_Class ::
> add_magazine(const Reference::Base& ref)
> {
> return 1;
> }
>
>
> int
> My_Class ::
> add_reference(const Reference::Base& ref)
> {
> int id(0);
> for (unsigned int i = 0;
> (id == 0) && (i < My_Class::NUM_ADD_FUNCS);
> ++i)
> {
> // Need to use ->*
> id = (this->*add_func_table[i])(ref);
> }
> return id;
> }
>
> int main()
> {
> }
>
> HTH
>
> Rob.

Thanks for the suggestion. I've tried it and it worked.
However, I was hoping for a "const" table rather than
a static one.

-- 
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


Relevant Pages