473,231 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Inheritance & static members

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
5 2029
* 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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: porneL | last post by:
How do I use static functions/properties with inheritance? I've found it very problematic. class Foo { static function A() {/* ??? */::B(); } static function B() {echo 'Foo';} }; class...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
64
by: groups | last post by:
C# is an impressive language...but it seems to have one big limitation that, from a C++ background, seems unacceptable. Here's the problem: I have a third-party Document class. (This means I...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
18
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
2
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.