Don't know how to code this. I'm new to C++.

This is a discussion on Don't know how to code this. I'm new to C++. within the C++ Programming forums, part of the General Programming Boards category; Iím starting to use C++ and I have some problems I hope you people can help me. I have the ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Don't know how to code this. I'm new to C++.

    Iím starting to use C++ and I have some problems I hope you people can help me.
    I have the following classes on their own file.

    Code:
    /**
     * Basic application class.
     * Any application needs this.
     */
    class App
    {
    public:
    	~App(); /**< Destructor. */
    
    	int init(); /**< Application initiation. */
    	void checkExtensions(); /**< Check that required extensions are available. */
    
    	void idle(); /**< Function called every frame. */
    	void render(); /**< Render basic info, etc. */
    
    	void keyPress(int key); /**< Check if a valid key was pressed. */
    };
    Code:
    /**
     * Class that deals with Win32 specific stuff.
     * This includes mouse handling, basic loop, window creation, etc.
     */
    class WinApp
    {
    public:
    	WinApp(); /**< Constructor. */
    	~WinApp(); /**< Destructor. */
    
    	int setVideoMode(int width, int height, int fs = 0); /**< Set the video mode for the application. */
    	void setTitle(const char *title); /**< Set the title of the window. */
    
    	void checkExtension(const char *extension); /**< Check if a needed extension is available. */
    	void error(); /**< Check if there was any error. */
    
    	void main(); /**< Deal with Window's messages, main loop. */
    
    	int windowWidth; /**< Width of the window. */
    	int windowHeight; /**< Height of the window. */
    	int fullScreen; /**< Fullscreen. */
    	char title[1024]; /**< Title of the window. */
    
    	int mouseX; /**< Location of the mouse pointer along the X axis. */
    	int mouseY; /**< Location of the mouse pointer along the Y axis. */
    	int mouseButton; /**< Check what mouse button was pressed. */
    
    	float fps; /**< Frames per second. */
    };
    The application needs just one instance of App and one instance of WinApp. And that single instance of WinApp needs to be inside my single instance of App in such way that there is no WinApp without App, and in such way that WinApp can call functions of App. How do I do that without using inheritance? I was using inheritance and I didnít have any problem but it was blending everything too much, making it WinApp more of a part of App and not some ďservice moduleĒ which is what I want.

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    PS: Sorry for the name of the topic, I couldn't come up with anything better.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    And that single instance of WinApp needs to be inside my single instance of App...and in such way that WinApp can call functions of App. How do I do that without using inheritance?
    I don't think you can. If you make WinApp a nested class it won't have access to App. If you make WinApp a friend class of App, then WinApp can have functions that you pass an App to and WinApp can change the private members of that App. However, WinApp won't be able to call App functions. If you think about the syntax, how would that work? If you start like this:

    Code:
    WinApp wa;
    wa.         <----what goes next?  It has to be a WinApp member or function.
                     An App member or function will cause an error.
    Last edited by 7stud; 02-22-2005 at 09:42 PM.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    So what would be the best solution without using inheritance? I don't want to use inheritance because both classes are not that related, WinApp should some kind of "service module".

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >So what would be the best solution without using inheritance?

    Heh... I just wrote out an example of inheritance and then read your last post after finishing. All hail the backspace key.

    Anyway, for what you are saying here it seems like inheritance is actually exactly what you are looking for. So why not use it?

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by master5001
    Anyway, for what you are saying here it seems like inheritance is actually exactly what you are looking for. So why not use it?
    Cause many operations of App aren't really true for WinApp, it isn't a true reflection of what the class does, and I would be inheriting a whole load of data that isn't useful or appropriate for WinApp.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It sounds like you should make WinApp a member of the App class. WinApp doesn't pass the 'is a' test: WinApp 'is an' App? Nope. However, WinApp does pass the 'has a' test: App 'has a' WinApp.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    So if I add the following line to the App class in the public part
    Code:
    WinApp winApplication;
    and I access the functions of WinApp with for example
    Code:
    App *myApplication = new App();
    myApplication->winApplication.setVideoMode(windowWidth, windowHeight, 0);
    How do I call for example myApplication->idle() from the setVideoMode function that's inside WinApp?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How do I call for example myApplication->idle() from the setVideoMode function that's inside WinApp?
    Obviously App objects cannot call WinApp functions: App objects call App functions, and WinApp objects call WinApp functions. I think you need to study a little bit about classes and encapsulation. The whole point of having classes is to prevent non class objects from having access to class functions. If you don't want ecapsulation, then don't use classes.

    You also seem to be thinking about inheritance backwards. The general members and functions should make up the base class, and the derived class should add more detail to the base class. It sounds like WinApp, with its fewer functions, should be the base class and App, with its many more functions, should be the derived class.
    Last edited by 7stud; 02-24-2005 at 01:15 AM.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    So, after reading a lot I came up with a solution for my problem, it works great so maybe it will help other people that had the same problem (and that didn't read too much).

    I removed the inheritance and for my App and WinApp classes I made the following function:
    Code:
    static App * Instance()
    {
    	static App instance;
    	return &instance;
    }
    Now I just use the following kind of line to access what I want:
    Code:
    WinApp::Instance()-><whatever here>
    .
    Thanks for the help master5001 and 7stud.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It appears that you've turned App into a sort of pseudo-Singleton class (look up singleton on google if you don't know what it means). If you only ever want one object of each of App and WinApp to be created, then this will work fine for you; but you can have a WinApp as a member of App (as you had before), and have an App* as a member of WinApp, and pass this to WinApp's constructor when it is created from the App object's constructor - and then initialize the App* in WinApp's constructor to the pointer passed by App's constructor.
    Code:
    class App;  //Forward declaration
    class WinApp
    {
    public:
       WinApp(App* pApp) :mpApplication(pApp) {}
       void doSomething() { mpApplication->doSomethingElse(); }
       void bar() { cout << "member of WinApp"; }
    protected:
       App* mpApplication;
    };
    
    class App
    {
    public:
       App() :mWinApplication(this) {}
       void foo() { mWinApplication.bar(); }
       void doSomethingElse() { cout << "Member of App"; }
    protected:
       WinApp mWinApplication;
    };
    Just an idea.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Thanks for the idea Hunter2, I came up with the pseudo-Singleton solution after realizing that there should never be more than one instance of App which means that there should never be more than one instance of WinApp so I got away by just using singletons for both and removing the WinApp member in App.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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