Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by csonx_p Mhh... I lost you, set it to/with what exactly... is this using the 'new' operator ? ...

  1. #256
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Mhh... I lost you, set it to/with what exactly... is this using the 'new' operator ?
    Code:
    FleetMenu *menuPtr = new FleetMenu();
    Yes, either that or the address of a global/static variable - don't forget to free at the end of your application if you use new.

    --
    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.

  2. #257
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by csonx_p View Post
    Mhh... I lost you, set it to/with what exactly... is this using the 'new' operator ?
    Code:
    FleetMenu *menuPtr = new FleetMenu();
    OR

    Code:
    MenuContext ctxt;
    
    ctxt.menuPtr  = new FleetMenu();

  3. #258
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Yes, either that or the address of a global/static variable - don't forget to free at the end of your application if you use new.

    --
    mats
    Few questions to ask...

    Code:
    #include "FleetMenu.h"
    
    // Class MenuContext
    class MenuContext 
    {
    public:
    	bool fquit;
    	
    	FleetMenu *menuPtr; 
    
    	MenuContext() : fquit(false) {}
    	~MenuContext() {};
    };
    1. How would the statement ...
    Code:
    FleetMenu *menuPtr = new FleetMenu();
    ... 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.

    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 ...

    Code:
    function(MenuContext &ctxt)
    {
          ctxt.menuPtr = new FleetMenu();
    }
    then i don; have to worry about the first question?

  4. #259
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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 use
    Code:
    FleetMenu *menuPtr = new FleetMenu();
    Then you would want to pass menuPtr to the MenuContext constructor and store a copy of the pointer in the MenuContext object being constructed.

    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.

  5. #260
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    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 use
    Code:
    FleetMenu *menuPtr = new FleetMenu();
    Then you would want to pass menuPtr to the MenuContext constructor and store a copy of the pointer in the MenuContext object being constructed.

    There are other ways to do this, particularly the SingleTon pattern may be a good fit here.

    --
    Mats
    Becoming a little complex but i like learning, specially if it will make things work easier...

    Alright, here's an example of singleton

    Code:
      class Singleton 
      {
      public:
          static Singleton* Instance();
      protected:
          Singleton();
          Singleton(const Singleton&);
          Singleton& operator= (const Singleton&);
      private:
          static Singleton* pinstance;
      };
    Q.
    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?

  6. #261
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  7. #262
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    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
    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?

  8. #263
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    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.

  9. #264
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by csonx_p View Post
    My question was why use this with stdin.... Is it to allow all types and size of data to be ignored/cleared? Where else are boosts commonly used? a simple example maybe!
    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.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 08:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2002, 12:21 AM

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