473,626 Members | 3,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating table of const ptr to member funcs

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(c onst Reference::Base & ref);

private:
int add_book(const Reference::Base & ref);
int add_magazine(co nst 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_f unc_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(co nst Reference::Base & ref)
{
// Stubbed for now.
return 1;
}
int
My_Class ::
add_reference(c onst Reference::Base & ref)
{
int id(0);
for (unsigned int i = 0;
(id == 0) && (i < NUM_ADD_FUNCS);
++i)
{
id = add_func_table[i](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.l earn.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

Jul 19 '05 #1
6 3478
Thomas Matthews wrote in news:3F******** ******@sbcgloba l.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::Bas e const& ref)
;
int add_reference(c onst Reference::Base & ref);

private:
int add_book(Refere nce::Base const& ref);
int add_magazine(Re ference::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_f unc_table[] =
{
&My_Class::add_ book, &My_Class::add_ magazine
};

//Now a member
unsigned int const My_Class::NUM_A DD_FUNCS =
sizeof(My_Class ::add_func_tabl e)
/
sizeof(My_Class ::add_func_tabl e[0])
;

int
My_Class ::
add_book(const Reference::Base & ref)
{
return 0;
}

int
My_Class ::
add_magazine(co nst Reference::Base & ref)
{
return 1;
}
int
My_Class ::
add_reference(c onst Reference::Base & ref)
{
int id(0);
for (unsigned int i = 0;
(id == 0) && (i < My_Class::NUM_A DD_FUNCS);
++i)
{
// Need to use ->*
id = (this->*add_func_tabl e[i])(ref);
}
return id;
}

int main()
{
}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2
在 Sun, 02 Nov 2003 20:41:19 GMT 时, Thomas Matthews
<Th************ *************** *@sbcglobal.net > 写了:
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(c onst Reference::Base & ref);

private:
int add_book(const Reference::Base & ref);
int add_magazine(co nst 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_f unc_table[] =
{
add_book, add_magazine
};
static const unsigned int NUM_ADD_FUNCS =
sizeof(add_func _table) / sizeof(add_func _table[0]);


i'm now find a way to do this, but i do not know is this you want,
code:
class My_Class
{
public:
typedef int (My_Class:: * P_ADD_REC_FUNC) (const Reference::Base &
ref);
int add_reference(c onst Reference::Base & ref);

private:
int add_book(const Reference::Base & ref);
int add_magazine(co nst Reference::Base & ref);
//static const P_ADD_REC_FUNC add_func_table[];
// const P_ADD_REC_FUNC add_func_table[];
//change to :
const P_ADD_REC_FUNC * add_func_table;

// and add the constructor like this:
My_Class(const P_ADD_REC_FUNC ptr_to[]) : add_func_table( ptr_to) {}
};

Jul 19 '05 #3
Rob Williscroft wrote:
Thomas Matthews wrote in news:3F******** ******@sbcgloba l.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::Bas e const& ref)
;
int add_reference(c onst Reference::Base & ref);

private:
int add_book(Refere nce::Base const& ref);
int add_magazine(Re ference::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_f unc_table[] =
{
&My_Class::add_ book, &My_Class::add_ magazine
};

//Now a member
unsigned int const My_Class::NUM_A DD_FUNCS =
sizeof(My_Class ::add_func_tabl e)
/
sizeof(My_Class ::add_func_tabl e[0])
;

int
My_Class ::
add_book(const Reference::Base & ref)
{
return 0;
}

int
My_Class ::
add_magazine(co nst Reference::Base & ref)
{
return 1;
}
int
My_Class ::
add_reference(c onst Reference::Base & ref)
{
int id(0);
for (unsigned int i = 0;
(id == 0) && (i < My_Class::NUM_A DD_FUNCS);
++i)
{
// Need to use ->*
id = (this->*add_func_tabl e[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.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #4
Thomas Matthews wrote in news:20upb.2432 4$0Z4.16958
@newssvr31.news .prodigy.com:
Thanks for the suggestion. I've tried it and it worked.
However, I was hoping for a "const" table rather than
a static one.

This should be possible, I put static in because of this initializer:
const P_ADD_REC_FUNC My_Class::add_f unc_table[] =
{
add_book, add_magazine
};


What role is "const" fulfilling. The above suggested to me that you
wanted a fixed table, since you say you don't want a static member
table, I guess you want to be able to create several My_Class
objects, all with different tables.

Perhapse a member with type std::vector< P_ADD_REC_FUNC_ CONST >
would be a good place to start, or maybe:

struct My_Class
{
typedef
int (My_Class:: * const P_ADD_REC_FUNC_ CONST)
(Reference::Bas e const& ref)
;

My_Class( P_ADD_REC_FUNC_ CONST *table, std::size_t count ) :
add_fun_table( table ), add_fun_table_c ount( count )
{}

P_ADD_REC_FUNC_ CONST * const add_func_table;
std::size_t const add_fun_table_c ount;

int add_book(Refere nce::Base const& ref);
int add_magazine(Re ference::Base const & ref);
};

My_Class::P_ADD _REC_FUNC_CONST global[] = {
{ &My_Class::add_ book, &My_Class::add_ magazine }
};
My_Class my_instance( global, 2 );

If you could give a simple usage scenario for My_Class it might help.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5
Rob Williscroft wrote:
Thomas Matthews wrote in news:20upb.2432 4$0Z4.16958
@newssvr31.news .prodigy.com:

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

This should be possible, I put static in because of this initializer:

const P_ADD_REC_FUNC My_Class::add_f unc_table[] =
{
add_book, add_magazine
};

What role is "const" fulfilling. The above suggested to me that you
wanted a fixed table, since you say you don't want a static member
table, I guess you want to be able to create several My_Class
objects, all with different tables.
If you could give a simple usage scenario for My_Class it might help.

Rob.


In the big picture, "My_Class" is a singleton interface to a database
of references. There are many relational tables to contain the data.
I'm using the interface to store a reference into the database (thus
the "add_*" methods) and to create references from the database.

I was hoping to have a const table of pointers to member functions
because the pointers to the functions would not change (constant
data). The quantity of pointers also doesn't change. Thus the
requirement for a constant table.

Every reference has a name and a category. The category is used
to determine which table the data is stored in. For example, the
data for a "Book" reference would be placed into the book table,
and the data for a "Magazine" reference would be placed into the
magazine table. Instead of having one public "add_" method for
each category, I'm using one public method for the base class.

--
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.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #6
Thomas Matthews wrote in news:xzwpb.1712 3$w%4.13133
@newssvr33.news .prodigy.com:
In the big picture, "My_Class" is a singleton interface to a database
of references. There are many relational tables to contain the data.
I'm using the interface to store a reference into the database (thus
the "add_*" methods) and to create references from the database.

I was hoping to have a const table of pointers to member functions
because the pointers to the functions would not change (constant
data). The quantity of pointers also doesn't change. Thus the
requirement for a constant table.
What I don't understand here is why not a static table.

Every reference has a name and a category. The category is used
to determine which table the data is stored in. For example, the
data for a "Book" reference would be placed into the book table,
and the data for a "Magazine" reference would be placed into the
magazine table. Instead of having one public "add_" method for
each category, I'm using one public method for the base class.


You say base class so presumably you are creating a singleton
for each category (Book, Magazine). Maybe the following will help

#include <iostream>
#include <ostream>

template < typename T >
struct Basic
{
// all "base class" code here
static int const array[];
static std::size_t const array_size;

int lookup( std::size_t i )
{
// possibly do something "non-static" here ...
return array[ i % array_size ];
}
};
template < typename T > std::size_t const Basic< T >::array_size =
sizeof( Basic< T >::array ) / sizeof( int )
;
// just change (define) the tables for each specialization
struct BookTag {};

template <> int const Basic< BookTag >::array[] = { 1, 2, 3 };

struct MagazineTag {};

template <> int const Basic< MagazineTag >::array[] = { 4, 5, 6, 7 };

int main()
{
using namespace std;

Basic< BookTag > bb;
cerr << "Book size: " << bb.array_size << endl;
cerr << "Book lookup 1: " << bb.lookup( 1 ) << endl;

Basic< MagazineTag > bm;
cerr << "Magazine size: " << bm.array_size << endl;
cerr << "Magazine lookup 1: " << bm.lookup( 1 ) << endl;

}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #7

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

Similar topics

11
2518
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find the best match. Anyone could give a detail explanation in theory? Which one is good?
7
62286
by: Diane | last post by:
Hi everybody. Does anyone have any sample hash table implementation for separate chaining? I'm trying to implement it myself, but I'm stuck. Thanks!
3
1836
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is not ever instantiated (thus no constructor or destructor ever called), but instead, all of its member variables are defined as static in the according .cpp file, and it's methods are invoked by calling foo::bar(...) to invoke method "bar(...)".
5
5478
by: Marcin Gil | last post by:
Hi! I have the code like this (obvious things like ctor/dtor removed) typedef struct _NODE { int val; int index; } Node;
3
2275
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the ctors/dtors etc for brevity sake) struct MyStructA
6
2160
by: Chris Carlen | last post by:
Hi: I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP. A module (.c file) contains external (to the funcs in that module) variables which are used to affect the operation of the program (other modules). These variables are only read by other parts of the program, but are to be set by a user interface which consists of an RS-232 terminal command line interface. The command parser for this interface responds to...
2
2996
by: FFrozTT | last post by:
I am having a problem creating a DLL with an entry point. I've been trying sub Main, DllMain, and I get nothing. When I run dumpbin - exports mydll.dll I see no entry points, also the dll when executed by rundll32 is not giving expected results. I have the project set to Class Library with Sub Main as the startup object, which I can't change to anything else anyway without modifying the .vbproj file which also seems to have no effect. ...
17
7833
by: DeZZar | last post by:
Hi all, I need to regularly backup my database as an Excel file and have been using the File Export option. Problem is I need anyone using the database to be able to do this easily - nopt just me. For all users the database is hidden and only a menu screen is visable. Can I create a button on my menu screen that will export a table >
6
26298
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this one table. My problem is I have two buttons I want to use this code for for the two buttons would put the data in different tables. I have tried copying and changing a few things and nothing will work for me. The code is set up in a module and then I...
0
8199
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8505
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.