Inheritance & static members

From: Thomas Matthews (Thomas_MatthewsSpamBotsSuck_at_sbcglobal.net)
Date: 02/29/04


Date: Sun, 29 Feb 2004 16:13:53 GMT

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


Relevant Pages

  • Re: paternal age
    ... Our family has lived in the rural areas that might form Barrows ... 30 seems to be decision time around here.. ... just rear the child. ... StrongArm Under construction, FAQ, recipes, tips, booklist, links ...
    (uk.business.agriculture)
  • Re: >Child Porn Trading FAQ<
    ... CHILD PORNOGRAPHY FAQ ... How and where to find real child porn ...
    (uk.legal)
  • Re: Media and legal cover up of women child abusers
    ... common. ... For a small child being towered over by an adult over twice it's height ... You appear to be saying that abuse is so commonplace that most of us ... have suffered childhood abuse. ...
    (uk.legal)
  • >Child Porn Trading FAQ<
    ... CHILD PORNOGRAPHY FAQ ... How and where to find real child porn ...
    (uk.legal)
  • Child-Porn Kiddy-Porn Trading FAQ
    ... CHILD PORNOGRAPHY FAQ ... How and where to find real child porn ...
    (alt.true-crime)