![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2007
Posts: 24
Rep Power: 0
![]() |
programming design
Hello
I have a programming design question.. Lets say I have 3 classes like below: 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: class session {
public:
std::list<user> m_list;
parser m_parser;
};Thanks for help Regards |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Why don't you make a parser object a member of a user object?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,126
Rep Power: 5
![]() |
Dunno if it fits with your design requirements, but have you considered making parser a static class?
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Mar 2007
Posts: 24
Rep Power: 0
![]() |
This would take too much memory. parser class has some data that doesnt need to be multiplied and it has only one function that is being called with different arguments.
Quote:
class user {
public:
user(int somedata) : m_parser(somedata) { }
static class parser m_parser;
}; |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
In that case, there is no need for your parser to be a class at all. It would be better implemented as a single function that is called, with arguments providing necessary data, by your user classes.
|
|
|
|
|
|
#6 | |
|
Newbie
Join Date: Mar 2007
Posts: 24
Rep Power: 0
![]() |
Quote:
And I dont like having global objects/functions. |
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You seem to have all the answers. One wonders why there is a question.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,126
Rep Power: 5
![]() |
Quote:
That way, you group one or more functions into your class, and can call them like regular global functions. The class can have an internal state, such that it can initialize itself when the first method is called, or upon calling some initialization method (for example, to 'attach' the stream std::cin as the source for user input).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
Not a valid justification for using a class rather than a single function. Simply pass the needed data as an argument (eg a struct, a pointer to a struct, a reference to a struct) to the function.
Lots of people don't like eating vegetables either. But people who eat vegetables are usually healthier than those who don't. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| The Black Art of Video Game Console Design | lostcauz | Book Reviews | 0 | Apr 26th, 2006 8:31 PM |
| Does Programming Make You Smarter? | Sane | Coder's Corner Lounge | 43 | Oct 2nd, 2005 7:12 AM |
| Design / Programming Community | Yuneek | Existing Project Development | 15 | May 7th, 2005 10:41 AM |