By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

Inheritance & static members

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* Thomas Matthews <Th*************************@sbcglobal.net> schriebt:

/* 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.

Jul 22 '05 #2

P: n/a
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:40**************@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

Jul 22 '05 #3

P: n/a
al***@start.no (Alf P. Steinbach) wrote in message news:<40****************@news.individual.net>...
* Thomas Matthews <Th*************************@sbcglobal.net> schriebt:

/* 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.


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.
Jul 22 '05 #4

P: n/a
Alf P. Steinbach wrote:
* Thomas Matthews <Th*************************@sbcglobal.net> schriebt:
/* 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.


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

Jul 22 '05 #5

P: n/a
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

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.