some design questions

This is a discussion on some design questions within the C++ Programming forums, part of the General Programming Boards category; If you have a class customer (for instance) which has a container std::vector<std::string> that stores customer's nicknames and you want ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,318
    If you have a class customer (for instance) which has a container std::vector<std::string> that stores customer's nicknames and you want to access it from other classes so that you can get the first nickname in the list, x-th name in the list and you want to be able to add nicknames to the list - how would you implement it?
    Perhaps you could write something like this:
    Code:
    class customer {
    public:
        typedef std::vector<std::string>::size_type size_type;
    
        void add_nickname(const std::string& nickname)
        {
            m_nicknames.push_back(nickname);
        }
    
        std::string get_nickname(size_type n) const
        {
            return m_nicknames[n];
        }
    
        size_type num_nicknames() const
        {
            return m_nicknames.size();
        }
    private:
        std::vector <std::string> m_nicknames;
    };
    After all, if you want to know what is the first nickname, you can just call customer_obj.get_nickname(0).

    Maybe using boost:: optional would be better for get_nickname functions (because list could be empty) ?
    You could simply require the caller to check that the customer has nicknames before trying to access the nicknames. This could be enforced by returning m_nicknames.at(n) instead so as to throw an out_of_range exception if necessary.

    What do you think about those long function names? On one side it is sensible so you can be sure what function does, on other side it doesnt 'look good'..
    The "customer" seems unnecessary since this is with respect to a class named customer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It sounds like the other code needs to iterate through the list of nicknames. If so, I might return a copy (rather than a reference) of the vector of nicknames and let them iterate through that. My objection to the previous idea was that you mentioned reference, which would allow users of the class to change the contents.

    I think returning a const reference or a copy of the local vector is good for get, and then you can keep your add function. If the vector is empty the user of the class can just check the returned vector, and if they want to iterate through it they can do that themselves. However, you would still be encapsulating the modification of the vector.

    As far as the names go, long names are fine. In this case, I think the "customer" part of the names are redundant, you can remove them. So get_all_nicknames() might be better to return the vector, and add_nickname() might be better for the add. The customer part is implied since it is a member function of the customer class.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Well, I think it looks like a sensible implementation. Although the get functions could probably return a const reference to avoid copying. And they should probably be const too.
    I wouldn't be that worried about long function names. It's better they're descriptive than short and undescriptive. There's a file line, of course, but I think this is alright. Or you could shorten them to get_first_nicknname, get_nickname, etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,318
    I think returning a const reference or a copy of the local vector is good for get, and then you can keep your add function. If the vector is empty the user of the class can just check the returned vector, and if they want to iterate through it they can do that themselves. However, you would still be encapsulating the modification of the vector.
    hmm... that brings to mind something: returning a const reference or a copy of the local vector fixes the underlying implementation to a vector (unless one wants to waste time with some kind of translation to a vector). In view of an assumption that "the other code needs to iterate through the list of nicknames", it may be better in such a case to return an iterator instead, e.g.,
    Code:
    class customer {
    public:
        typedef std::vector<std::string>::const_iterator const_nickname_iterator;
    
        void add_nickname(const std::string& nickname)
        {
            m_nicknames.push_back(nickname);
        }
    
        const_nickname_iterator nickname_begin() const
        {
            return m_nicknames.begin();
        }
    
        const_nickname_iterator nickname_end() const
        {
            return m_nicknames.end();
        }
    private:
        std::vector<std::string> m_nicknames;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> returning a const reference or a copy of the local vector fixes the underlying implementation to a vector

    True. The iterator version is probably best. Returning the vector itself is probably simpler, especially since I assume the nickname is one of many properties of the customer. However, the iterator version is pretty easy and better design.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. some questions about design
    By jinhao in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2005, 08:51 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  4. are you bored? (interiour design questions)
    By maes in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-04-2004, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21