Inheritance & static members

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
 
A

Alf P. Steinbach

* Thomas Matthews said:
/* 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 best answer is probably to forget about classes for the moment. In
any design classes that 'do' indicate some misunderstanding somewhere.
Except for functors, and you don't have functors.

If you absolutely must have classes, please rethink the decision to have
'Name_Lookup' as a common base class.

Instead (if you absolutely must have classes), consider having 'Table'
as a class instead of as a typedef, and put the 'add_name' method there.
 
D

David White

Thomas Matthews said:
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
 
G

Geetesh

The best answer is probably to forget about classes for the moment. In
any design classes that 'do' indicate some misunderstanding somewhere.
Except for functors, and you don't have functors.

If you absolutely must have classes, please rethink the decision to have
'Name_Lookup' as a common base class.

Instead (if you absolutely must have classes), consider having 'Table'
as a class instead of as a typedef, and put the 'add_name' method there.

instead of creating a base class. why dont you create a base class
using templates and in that template base class have table as static.
 
T

Thomas Matthews

Alf said:
The best answer is probably to forget about classes for the moment. In
any design classes that 'do' indicate some misunderstanding somewhere.
Except for functors, and you don't have functors.

I don't understand what you are stating.
Please give an example.
If you absolutely must have classes, please rethink the decision to have
'Name_Lookup' as a common base class.

Instead (if you absolutely must have classes), consider having 'Table'
as a class instead of as a typedef, and put the 'add_name' method there.

Actually, I have Table as an abstract interface so that I can use
different Database Engines to implement the table.

What I am looking for is a design to hide the actual information.
For example, a Category could be a string, or it could be a
string in a table. The client using Category should not be
concerned about the implementation of Category.

I would like to have separate tables for Category, Author and
Publisher. For example, a Book has a category, an author
and a publisher. A Magazine has a category and a publisher.
Both books and magazines are references. All references
have a minimum of a category and a name.

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

Thomas Matthews

David White wrote:

[snip]
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?

I want a static table because I only need one table. I'm writing a
reference database system. A book, magazine, and newspaper all have
a publisher. To reduce duplicate information, the publisher
information is extracted to a separate table. There only needs to
be one instance of the publisher table. The book, magazine and
newspapers will have a handle into the publisher table.

I want all instances of books to have a pointer to the publisher
table. No problem here.

Now, more than one reference type has an author. Again, the
author details are extracted to a separate table. No problem.

The Author and Publisher tables share a common structure.
However, there should only be one instance of each table.

Perhaps the better method is to have a Database class which
insures only single instances of each table.


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top