Thread: programming design (another thread)

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

    programming design (another thread)

    Hello

    I have another programming design question..
    Lets say I have 3 classes like below:

    Code:
    class parser {
    };
    
    class user {
    };
    
    class session {
    public:
    	std::list<user> m_list;
    };

    Session class is a 'main class' where most of the work is being done (in loop/s)..
    It has a list of user objects, where each user needs access to 1 particular parser object (parser class).

    I dont want each user to have its own parser object since that would be unneccessary..

    I wonder what is the best approach to make class user (user objects) be able to access data in one parser object?
    I know most of you would just pass parser object's reference to user class constructor, and then access data this way.

    Is there any better way/design pattern to achieve that?
    Maybe I shouldn't bother because this might be the best/easiest way?
    Or is there more 'professional' way?

    Another thing I'm unsure about..

    In case passing reference of parser object to user objects is the best way, where would you guys define parser object?

    Does this seem okay:

    Code:
    class session {
    public:
    	std::list<user> m_list;
    	parser m_parser;
    };
    Thanks for help
    Regards

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is there one parser per session? Does the parser need to remain alive through multiple calls of session's functions? How many user functions require the parser? Does those functions require the same parser through different invocations?

    You could make the parser local to the session function that uses it if you only need it for that small lifetime.

    You could pass the parser by reference to the user functions instead of the class itself, if the user only needs it for certain functions.

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    How about giving the user class a static parser object?
    Code:
    class user
    {
    private:
        static parser yournamehere;
    };
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM