T
Thomas Matthews
Hi,
I have three classes:
Category
Author
Publisher
Each of these classes stores its information into
a database table of <ID, text>. (They are fields
that have a foreign key.) There is only one
table for each instance of these classes (IOW,
the table table is declared static in the classes).
typedef std::map<unsigned int, std::string> Table;
/* version 1 */ class Category
{
int id; // handle to table record.
static Table table;
public:
void add_name(const string& new_name);
};
/* version 1 */ class Author
{
int id; // handle to table record.
static Table table;
public:
void add_name(const string& new_name);
};
Due to this commonality, I've decided to create
a parent class, Name_Lookup, which contains the
common stuff (methods & members) among the three
classes.
/* version 1 */ class Name_Lookup
{
int id;
public:
void add_name(const string& new_name);
};
/* version 2 */ class Category
: public Name_Lookup
{
static Table category_table;
};
/* version 2 */ class Author
: public Name_Lookup
{
static Table author_table;
};
I know that if I declare a static table in
Name_Lookup, there will be only one table for
all the child classes; which is what I don't
want.
My dilemmas are:
1. Ensuring that each child of Name_Lookup has
a static table. {If the table is declared
in Name_Lookup, there will only be one table
for all child classes).
2. The common methods, defined in Name_Lookup,
need to access the tables in the the child
methods.
My solution, to #2, is to have a method in each
child class return a pointer to the static table:
/* version 2 */ class Name_Lookup
{
int id;
public:
void add_name(const string& new_name);
protected:
virtual Table * get_table(void) = 0;
}
/* version 3 */ class Category
: public Name_Lookup
{
static Table category_table;
protected:
virtual Table * get_table(void)
{ return &category_table;};
};
Any other solutions or suggestions?
--
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
I have three classes:
Category
Author
Publisher
Each of these classes stores its information into
a database table of <ID, text>. (They are fields
that have a foreign key.) There is only one
table for each instance of these classes (IOW,
the table table is declared static in the classes).
typedef std::map<unsigned int, std::string> Table;
/* version 1 */ class Category
{
int id; // handle to table record.
static Table table;
public:
void add_name(const string& new_name);
};
/* version 1 */ class Author
{
int id; // handle to table record.
static Table table;
public:
void add_name(const string& new_name);
};
Due to this commonality, I've decided to create
a parent class, Name_Lookup, which contains the
common stuff (methods & members) among the three
classes.
/* version 1 */ class Name_Lookup
{
int id;
public:
void add_name(const string& new_name);
};
/* version 2 */ class Category
: public Name_Lookup
{
static Table category_table;
};
/* version 2 */ class Author
: public Name_Lookup
{
static Table author_table;
};
I know that if I declare a static table in
Name_Lookup, there will be only one table for
all the child classes; which is what I don't
want.
My dilemmas are:
1. Ensuring that each child of Name_Lookup has
a static table. {If the table is declared
in Name_Lookup, there will only be one table
for all child classes).
2. The common methods, defined in Name_Lookup,
need to access the tables in the the child
methods.
My solution, to #2, is to have a method in each
child class return a pointer to the static table:
/* version 2 */ class Name_Lookup
{
int id;
public:
void add_name(const string& new_name);
protected:
virtual Table * get_table(void) = 0;
}
/* version 3 */ class Category
: public Name_Lookup
{
static Table category_table;
protected:
virtual Table * get_table(void)
{ return &category_table;};
};
Any other solutions or suggestions?
--
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