Re: Inheritance & static members

From: David White (no_at_email.provided)
Date: 03/01/04


Date: Mon, 1 Mar 2004 12:11:03 +1100


"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> wrote in
message news:40420F89.1020300@sbcglobal.net...
> 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.

The obvious question is: why do you want static tables at all? By
definition, a static member is a single member that belongs to the class and
exists independently of any instances of the class. Even if you intend to
have only one instance of each class, if you imagine having more than one,
would it make sense for all instances to use the same table? If not, then I
don't think it's appropriate for the tables to be static.

Perhaps your design is more obvious to those with more database experience
than I. I'm not sure how the id member is used. Are going to have one
instance of each class and increment the id each time you add to the table,
or will there be once instance for every name you will add to the table?

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

Here's one:

#include <string>
#include <map>

class Database
{
private:
    Table table;
    int id;
public:
    Database();
    void add_name(const std::string &name);
};

int main()
{
  Database authors;
  Database categories;
  // and so on
}

Here's another that's closer to your design, but which I don't like because
of the static member:

#include <string>
#include <map>

typedef std::map<unsigned int, std::string> Table;

enum TableType { AUTHOR, CATEGORY };

template<TableType> class Database
{
private:
    int id;
    static Table table;
public:
    void add_name(const std::string &name);
};

template<TableType tt> Table Database<tt>::table;

int main()
{
  Database<AUTHOR> authors;
  Database<CATEGORY> categories;
  // and so on
}

You get a different static table for each different value of the enum, but
the same static table for the same value of the enum, so it's the same as
your original design but there is now no need for a common base class.

DW



Relevant Pages