Jump to content
  • Advertisement

HiMK

Member
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

106 Neutral

About HiMK

  • Rank
    Newbie
  1. Hello   I am creating a boiler-plate namespace that will allow me to ensure proper coding practices are followed and avoid gotchas. Is this strategy reccommended? Looking over my code below; can you see any "no-no's" or gotchas? Can you also provide advice on anything I should do differently or add?    #ifndef PROJECT_INTERFACES_H_ #define PROJECT_INTERFACES_H_ // Ensure proper coding conventions are followed namespace ProjectInterfaces { class IUnitTest { protected: friend class RegressionTest; virtual bool unitTest() const = 0; }; template <class T> class INonCopyable { protected: INonCopyable() { } ~INonCopyable() { } private: INonCopyable(const INonCopyable& iNoCopy); // = delete; C++11 INonCopyable& operator=(const INonCopyable& iNoCopy); // = delete; INonCopyable(INonCopyable&& iNoCopy); // = default; INonCopyable& operator=(INonCopyable&& iNoCopy); // = default; }; template <typename T> class IMapKey { public: virtual bool operator<(const T& other) const = 0; }; } #endif // PROJECT_INTERFACES_H_ Code usage:       // Should these use virtual inheritence? class MyClass : private INonCopyable<MyClass>, public IUnitTest, public IMapKey<MyClass> { public: MyClass(const int& value) : value_(value) { } virtual bool operator<(const MyClass& other) const override { return value_ < other.value_; } const int getValue() const { return value_; } protected: virtual bool unitTest() const override { // ... return true; } private: int value_; };
  2. I am very much interested in using smart pointers so I'd appreciate any advice especially if shared_ptrs should/can be cast to weak_ptrs. This is not homework, this is a practice project from here: http://www.cppforschool.com/projects.html If I wanted to copy and paste code I would just copy the code they supply there and not post here. As you'll see I am taking a far more strict approach to memory management and general design for my own benefit and knowledge.   What are the complications that can occur from using explicit?   Please elaborate on data redundancy and where this occurs in the code. I haven't come across this term before and googling didn't clear up where I am making this mistake.
  3. Thanks for the advice. So regarding unicode strings what libraries do you guys use? Boost C++?
  4. Hello   I am doing a beginner C++ project and its simply a Library Mangement System where a student will hire, give back a book, book and hire records are stored in a binary file etc.   I'm approaching the project as though the project would be used by other developers and that I need to design the classes so they cannot be incorrectly used, the project can be scaled/extended at a later date easily and avoid gotchas/bugs down the track so I am being very strict (overkill almost) with my implementation.   Therefore, I have some features of the LibraryBook class that I want to implement but dont quite know if this is the best way of doing. If you could provide advice below it would be much appreciated.   Classes: - LibraryManagementSystem (LMS) - LibraryBook - Student   LibraryBook features I want to implement: - The class has the expected private properties of title, author, availability, id. I have made the LMS a friend class so that it and only it can change the books availability, maybe even author, title (for scalability/extendability). Would you use friend here or maybe another technique? - A book object should only ever be created by the LMS, this makes sense I guess because a Student shouldn't be able to make a new book only the LMS. So I was thinking of making the LibraryBook class private but that means a Student wont even be able to hold a book (as a property in their class). I am thinking I should only ever give a handle of the book to the Student? Maybe this is the case for the application of the factory design pattern. - A LibraryBook must have its title, author, etc. defined always. So I have made the constructor explicit. Should I make the default constructor private? That way a book can only ever be created with defined title, author, etc.   LMS features I want to implement: - I want to store the LibraryBooks in a std::map <string, std::shared_ptr<LibraryBook>>. This is because a student will most likely ask/search for the book by title (title string = key), the LibraryBook dynamic memory will be automatically deleted and I can give the Student a weak_ptr to the LibraryBook which means they cant change anything on that object.   Knowing what I want to achieve above; can you provide advice on design patterns and etc. I should use and any errors I have made in my implementation below? class Student { public: // I dont think that hire book should be a student method but a LMS method because the LMS issues a book, creates a record etc. weak_ptr<LibraryBook> curBook; // is this ok as public? or should only the LMS be able to set a students book? Maybe this property only should be friend with LMS? protected: private: const string firstName; const string lastName; const unsigned int ID; }; class LibraryBook { public: friend class LibraryManagementSystem; protected: private: explicit LibraryBook(string nTitle, string nAuthor) : title(nTitle), author(nAuthor) { } // will this mean noone not even LMS can create a book using the default constructor? They have to use this one only? LibraryBook(); // No need to implement destructor because implicitly created and theres no dyn memory in this class LibraryBook(const LibraryBook& lB); // dont implement to disallow copying a book void operator=(const LibraryBook& lB); // dont implement to disallow copying a book const string title; const string author; const unsigned int ID; }; class LibraryManagementSystem { public: static LibraryManagementSystem& Instance() { static LibraryManagementSystem instance; return instance; } weak_ptr<LibraryBook> hireBook(string bookTitle, Student& student) { weak_ptr<LibraryBook> book; // initialised as null ptr I think? if (libraryBooks.find(bookTitle) == libraryBooks.end()) // what should I return? An empty/null weak_ptr? book = libraryBooks[bookTitle]; // is this cast allowed/possible? Is it allowed to write/overwrite a weak ptr? student.curBook = book; // TODO: create LMS record of who has book, when due back, etc. return book; } protected: private: LibraryManagementSystem() {} ~LibraryManagementSystem() {} LibraryManagementSystem(const LibraryManagementSystem& lms); // dont implement to disallow copy constructor void operator=(const LibraryManagementSystem& lms); // dont implement assignment operator map<string, shared_ptr<LibraryBook>> libraryBooks; };
  5. Hello   I am challenging myself to try to attempt to write a function that is as efficient, portable and failsafe as possible. The function is very simple and just converts a string (or wstring) to lower case. Are there any improvements you think I could make and any errors I have done? #ifdef UNICODE typedef std::wstring tstring; typedef std::wstring::npos tnpos; #else typedef std::string tstring; //typedef std::string::npos tnpos; // throws compiler error?! #endif tstring toLower(const tstring& str) { // Alternative 1: Returns a copy of a local string so not that efficient. unsigned int len = str.length(); // use unsigned int so I only have 4 bytes as opposed to 16 for int tstring lower(len, 'a'); // ensure string is correct size to avoid dynamic resizing. Reserve at construction; performing 2 steps at once defintion and reserving - is faster right? // or use lower.reserve(len); for (unsigned int i=0; i<len; i++) // iterate using indexes. Maybe iterators could be more fail safe/efficient/etc.? lower[i] = tolower(str[i]); return lower; } void toLower(const tstring& inStr, tstring& outStr) { // Alternative 2: Have user define string and pass by reference (this would be faster?) unsigned int len = inStr.length(); // use unsigned int so I only have 4 bytes as opposed to 16 for int. Store length in local variable because we look it up again in the below for loop outStr.resize(len); // ensure string is correct size to avoid dynamic resizing. for (unsigned int i=0; i<len; i++) // iterate using indexes. Maybe iterators could be more fail safe/efficient/etc.? outStr[i] = tolower(inStr[i]); }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!