Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Few questions to ask...
1. How would the statement ...Code:#include "FleetMenu.h" // Class MenuContext class MenuContext { public: bool fquit; FleetMenu *menuPtr; MenuContext() : fquit(false) {} ~MenuContext() {}; };
... outside menucontext say in FleetMenu.cpp initialize the member (menuPtr) of menucontex? Besides, why can't i just do this inside menucontext as i declare is, by this i won't have to worry every time i use it.Code:FleetMenu *menuPtr = new FleetMenu();
2. I get an error when i compile the above because: FleetMenu.h includes Menu.h which includes MenuContext.h... So i'm including FleetItem in MenuContext but FleetMenu which also includes MenuContext.h through Menu.h....
3. Since menucontext is passed to all my Menu functions, can't i just do the following in each of the function ...
then i don; have to worry about the first question?Code:function(MenuContext &ctxt) { ctxt.menuPtr = new FleetMenu(); }
Right, you should only ever have ONE FleetMenu object [at least, I don't see any reason to have more]. So 3 is the wrong solution, as you would create a new FleetMenu object for each menu.
So, if you useThen you would want to pass menuPtr to the MenuContext constructor and store a copy of the pointer in the MenuContext object being constructed.Code:FleetMenu *menuPtr = new FleetMenu();
There are other ways to do this, particularly the SingleTon pattern may be a good fit here.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Becoming a little complex but i like learning, specially if it will make things work easier...
Alright, here's an example of singleton
Q.Code:class Singleton { public: static Singleton* Instance(); protected: Singleton(); Singleton(const Singleton&); Singleton& operator= (const Singleton&); private: static Singleton* pinstance; };
1. Is this your idea of singleton
2. Should this Class be MenuContext or FleetMenu?, if it's MenuContex, how does FleetMenu object fit in since the example itself has an object of its own class?
It depends. There is only ever need for ONE FleetMenu object and only one MenuContext object, so technically both of those can be singletons.
I'm a little bit confused about the purpose of the FleetMenu. It makes sense to have a collection of functions within a class. But I don't quite see why it should hold data or have functions that take parameters inside it. Those probably belong somewhere else (and possibly not all in the same place).
Note that no one said that object oriented design is easy, and what goes in which object is definitely not easy to decide, and the decision is often arbitrary, a compromise and/or subject to personal preferences.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
FleetMenu has all the functions which manages the following...
1. View the list of available cars
2. Sort the list
3. Search for a specific car in a database
4. Delete car from the database
5. Add new car in a database
Well, i'm thinking i should take most of these operation to class Car since they all deal with car... What do you think?
They are not Car class members, because they deal with the COLLECTION (a vector or similar) of Car objects. You may want a Fleet object, which has those functions.
By the way, item 2 seems like something that should be done [temporarily?] during the listing [and possibly with different sorting criteria]. Or perhaps you want to use a sorting type of container class (e.g a binary tree structure).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The thing is that cin.ignore takes an argument - the amount of data to get rid of and ignore. It also says that if the argument passed is equal to the maximum range of the type (short I believe?), then it will discard all data in the input buffer.
However, since a short can be different sizes depending on compiler and platform, it's better and more common to use integer traits such as boost to specify the maximum size the short can hold and pass it to the function.
Boost is very popular in other areas, as well. It provides implementations of smart pointers such as shared_ptr and weak_ptr (both part of TR1). Also provides a static array in C++ style (boost::array), also part of TR1.
Boost contains lots and lots of meta programming help, such as boost::mpl::if(_c).
It also provides common operators such as implementing operator + simply deriving from boost::addable (there are more operators). It can also make a class non-copyable by deriving from another class.
It contains common libraries for file system, threads, and more. It also contains regular expressions, very powerful lambda expressions and function binders.
A lot of this is above your current knowledge, but I would advise you check the boost site for a detailed explanation of the boost libraries and what they all can do.