Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Converting C to C++
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Daniel T., post: 2560804"] [i] // Libraries hold more than just books, but everything a // library holds must have a code associated with it. class Resource { string code; public: Resource( string code_ ): code( code_ ) { } virtual ~Resource() { } const string& getCode() const { return code; } }; struct has_code : unary_function<Resource, int> { string code; has_code( const string& code_ ): code( code_ ) { } bool operator()( const Resource& resource ) const { return resource.getCode() == code; } }; class Library { vector<Resource> resources; public: bool codeExists( const string& tcode ) const { return find_if( resources.begin(), resources.end(), has_code( tcode ) ) != resources.end(); } }; class Book : public Resource { // add whatever here }; I am inclined to make Library a full fledged class... bool findcode( const Library& lib, const string& tcode ) { for (Library::const_iterator it = lib.begin(); it != lib.end(); ++it) if ( it->getCode() == tcode ) return true; return false; } (a) If all you need to know is if something with the code 'tcode' exists, then return a 'bool' instead of an 'int'. (b) You don't need to cast the '*itor' into a 'Book&' (c) You don't need to use the 'compare' member-function, simply compare them with operator==. (d) 'b.getCode' needs parens after it... 'b.getCode()'[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Converting C to C++
Top