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

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

P: n/a
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
Sep 11 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sep 11, 6:55*am, Payne <inva...@invalid.invalidwrote:
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
Sep 11 '08 #2

P: n/a
Puppet_Sock wrote:
On Sep 11, 6:55 am, Payne <inva...@invalid.invalidwrote:
>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
Sep 11 '08 #3

P: n/a
On Sep 11, 1:23*pm, Payne <inva...@invalid.invalidwrote:
Puppet_Sock wrote:
On Sep 11, 6:55 am, Payne <inva...@invalid.invalidwrote:
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
Sep 12 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.