How should I implement a find() member function for this class?

P

Payne

Hello, I'm having trouble figuring out how to best explain my problem
but I hope I can make myself clear enough. Anyway, I'm doing an
assignment for school and in this one we're supposed to write a simple
"database" class. The interface of this database class supports adding
items, removing items, obtaining information about how many items are
stored etc. In the assignment the database is supposed to support only
storing items of a particular class (which we will write) but I was
thinking of doing a templated database class instead (our teacher
encourages us to go beyond the specification if our ideas are sound).

I haven't started writing any code yet but I'm having one problem:
The database interface has a find method that searches for an item based
on a parameter sent. The problem is that the find method is directly
tied to the class the database is supposed to store items of (the other
class we're supposed to write). If I make it templated so it can store
any kind of object, I need to write the find method in such a way that
the user can specify how searches should be implemented. How would I do
that? Some kind of function object?

I don't know if it's possible, but the ideal solution would be (I think)
to be able to search for items in the database in a number of ways but
having only one find function and not having to rewrite the database
class or the classes it stores when adding new ways of searching for
items. Say I want to store records and I have class representing a
record and I have my templated database. Then I would like to be able to
search for items matching, say, the artist of the record. Imagine I also
have another database, this one storing items representing cars. Now I
want to search for cars of a particular manufacturer and with lower
bound in bhp. And all this without having to change any classes
involved. So can I use functors here? Please advise. :)

If you didn't understand a thing I said, please give me an opportunity
to clarify.

- Payne
 
P

Puppet_Sock

Hello, I'm having trouble figuring out how to best explain my problem
but I hope I can make myself clear enough. Anyway, I'm doing an
assignment for school and in this one we're supposed to write a simple
"database" class. The interface of this database class supports adding
items, removing items, obtaining information about how many items are
stored etc. In the assignment the database is supposed to support only
storing items of a particular class (which we will write) but I was
thinking of doing a templated database class instead (our teacher
encourages us to go beyond the specification if our ideas are sound).
[snips]

Have you read about the standard container classes in
the standard library? You probably want to.
Socks
 
P

Payne

Puppet_Sock said:
Hello, I'm having trouble figuring out how to best explain my problem
but I hope I can make myself clear enough. Anyway, I'm doing an
assignment for school and in this one we're supposed to write a simple
"database" class. The interface of this database class supports adding
items, removing items, obtaining information about how many items are
stored etc. In the assignment the database is supposed to support only
storing items of a particular class (which we will write) but I was
thinking of doing a templated database class instead (our teacher
encourages us to go beyond the specification if our ideas are sound).
[snips]

Have you read about the standard container classes in
the standard library? You probably want to.
Socks

Umm, what has that got to do with anything? I don't think I ever said
how the database class would be storing its data internally (it will be
using a standard container class fyi), because it was information not
needed for my question. The database classes has more things in its
interface, but I didn't mention them in my OP because those methods
weren't relevant to my question. Just using one of the standard
containers without a front-end isn't possible to meet the specifications
of the assignment, but the database class will interface with one
internally.
Also, this is an assignment, I can't just do what I like but I can give
feedback to teacher about things I don't like.
Anyway, judging from my own attempt and from the lack of serious
replies, I've decided to follow the specification on this one.

- Payne
 
P

Puppet_Sock

Puppet_Sock said:
Hello, I'm having trouble figuring out how to best explain my problem
but I hope I can make myself clear enough. Anyway, I'm doing an
assignment for school and in this one we're supposed to write a simple
"database" class. The interface of this database class supports adding
items, removing items, obtaining information about how many items are
stored etc. In the assignment the database is supposed to support only
storing items of a particular class (which we will write) but I was
thinking of doing a templated database class instead (our teacher
encourages us to go beyond the specification if our ideas are sound). [snips]

Have you read about the standard container classes in
the standard library? You probably want to.
Socks

Umm, what has that got to do with anything?
[snip]

So, that would be a "no" then. Pretty much everything you
talked about is available through the standard containers.
Try reading about such things as algorithms, for example.
Socks
 

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
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top