473,769 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<unsign ed 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.l earn.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 2062
* Thomas Matthews <Th************ *************@s bcglobal.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 misunderstandin g 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************ *************@s bcglobal.net> wrote in
message news:40******** ******@sbcgloba l.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<unsign ed 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<unsign ed int, std::string> Table;

enum TableType { AUTHOR, CATEGORY };

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

template<TableT ype tt> Table Database<tt>::t able;

int main()
{
Database<AUTHOR > authors;
Database<CATEGO RY> 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************ *************@s bcglobal.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 misunderstandin g 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************ *************@s bcglobal.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 misunderstandin g 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.l earn.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.l earn.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
2514
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 Bar extends Foo
14
12919
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 all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
3
1753
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 later ported to java and Python for comparison. http://bplusdotnet.sourceforge.net/ for full details and code ]
6
1885
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" using the prototype property. I realize there are no classes in JS, that code therefore lives in objects instead of class definitions, and that "inheritance" must be achieved prototypically and not classically. That said, on with the code. Say I...
64
3488
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 can't change the Document class.) I want to extend this (inherit from Document) as MyDocument, adding new events and application-specific methods and properties.
23
4615
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
2959
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 definition is perfectly legal in other CLR languages. For example in C#; static class XXX
8
2889
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
2002
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 inherit constructors or static methods...." In the C# spec, 17.2.1 Inheritance i read the following:
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.