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
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