Thread: class/program design

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    class/program design

    I have class where I have settings stored.
    Theres also another class session that reads data from previous class (it reads settings).

    Class settings contains a list of available servers, which is needed by session class. Im unsure how should I store information in session class that needs to know which the previous server in a list was (so it can move to the next in a list when its needed).

    Should I store current_server variable with an iterator? Im unsure if they are suitable for this kind of purposes. Should it be better to store the server with an integer (the number in the list)?



    Next thing is that server list in settings class can be updated. I guess the best approach would be to inform session class that server list was updated, so that current_server is set to the first in the list.

    Does anyone have a better idea how to implement this?

    Thanks for help

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    a standalone class called settings sounds like a bad design choice. Settings for what..?

    You shouldn't store iterators as class members, since any modification of the list may invalidate the iterator. Aside from which, details like this should be hidden away from the outside world (storing pointers or iterators to implementation details of other classes is quite a breach of encapsulation)

    Are all the settings applicable to a session? if so, how about storing a settings member inside your session class?

    if you have certain settings which aren't relevent to a session, then you should consider seperating out those settings to a different class, otherwise the interaction between objects is likely to get messy.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    In case I decide to store settings needed by session class inside this class, I still have to store current_server variable in the class (different functions need this data)..

    Still I wonder what would be the best way to know the position of current server in the list?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Still I wonder what would be the best way to know the position of current server in the list?
    How do you decide which server to use? Perhaps you can just use std::stack or std::queue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by laserlight View Post
    How do you decide which server to use?
    By order.. It starts with the first one, when the work is done it moves to the next one and so on.. When it comes to the end of the list should go back to the first one..

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by laserlight View Post
    Perhaps you can just use std::stack or std::queue.
    Is there any container in which data will remain after calling function pop()? Queue and stack destroy element/item after pop();

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's the whole point of pop().

    What you want is a list rotate, right?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee View Post
    What you want is a list rotate, right?
    Yes.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Basically you want a list of data the rotates around, once it hits the end it goes back to the begining? If thats the case a circular linked list might do the trick.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or just a pop_front followed by a push_back, and always using the front element of the list.

    You could use a queue for that. Just push every element you pop back into the queue.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee View Post
    Or just a pop_front followed by a push_back, and always using the front element of the list.

    You could use a queue for that. Just push every element you pop back into the queue.
    That sounds like a good solution. However, I thought there should be a std container for this purpose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which design is better to wrap another class instance
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2008, 12:27 AM
  2. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 04:40 PM
  3. Opinions on new site design
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 01-21-2005, 01:34 PM
  4. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM