Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. One Instance of class

One Instance of class

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stan the man
    wrote on last edited by
    #1

    Hi. I am not sure about this. I have a Cedit derived class in a DLL. In this I want to add a member of a keypad class I made. But I would like that EVERY CEdit derived class will only use ONE copy of this keypad. Can this be done just by declaring the keypad as a static member. Then when something it is entered into the keypad, it will be sent to the perspective Cedit class. Can someone help me shed some light on this? Thanks ahead of time. Stan the man

    M realJSOPR 2 Replies Last reply
    0
    • S Stan the man

      Hi. I am not sure about this. I have a Cedit derived class in a DLL. In this I want to add a member of a keypad class I made. But I would like that EVERY CEdit derived class will only use ONE copy of this keypad. Can this be done just by declaring the keypad as a static member. Then when something it is entered into the keypad, it will be sent to the perspective Cedit class. Can someone help me shed some light on this? Thanks ahead of time. Stan the man

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      A static member would work well. You could keep a static reference count as well which tracks how many edit controls are in existence (initialize to 0). When an edit control is created increment the reference count. If the count is now 1, create the keypad. When an edit control is destroyed, decrement the count, and if it's 0, destroy the keypad. The edit controls can set another static variable when they gain focus, so the keypad knows which one to direct input to. Mark

      Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the number 3

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        A static member would work well. You could keep a static reference count as well which tracks how many edit controls are in existence (initialize to 0). When an edit control is created increment the reference count. If the count is now 1, create the keypad. When an edit control is destroyed, decrement the count, and if it's 0, destroy the keypad. The edit controls can set another static variable when they gain focus, so the keypad knows which one to direct input to. Mark

        Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the number 3

        S Offline
        S Offline
        Stan the man
        wrote on last edited by
        #3

        Is it possible for me to decalre the keypad class in the edit class with a static? I would like to have the edit class somehow manage the keypad. Ex: class Keypad { } class Edit { static Keypad; } ... Is there someway to do that? Or do I have to manage the keypad in my mainfrm class or something? This way, when I declare an edit class, the keypad is "built in" but only has one copy (for memory sake) even with many edit controls. The keypad will direct the inputs to the correct parent. Thanks.

        M 1 Reply Last reply
        0
        • S Stan the man

          Is it possible for me to decalre the keypad class in the edit class with a static? I would like to have the edit class somehow manage the keypad. Ex: class Keypad { } class Edit { static Keypad; } ... Is there someway to do that? Or do I have to manage the keypad in my mainfrm class or something? This way, when I declare an edit class, the keypad is "built in" but only has one copy (for memory sake) even with many edit controls. The keypad will direct the inputs to the correct parent. Thanks.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Like you've shown it is what I was thinking... class CKeypadWnd : public CWnd { }; class CKeypadEdit : public CEdit { static CKeypadWnd *pKeypadWnd; static int KeypadRefCount; static CKeypadWnd *pActiveKeypadWnd; ... }; ... // In the CPP file: CKeypadWnd *CKeypadEdit::pKeypadWnd = NULL; int CKeypadEdit::KeypadRefCount = 0; CKeypadWnd *CKeypadEdit::pActiveKeypadWnd = NULL; ... Mark

          Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the number 3

          1 Reply Last reply
          0
          • S Stan the man

            Hi. I am not sure about this. I have a Cedit derived class in a DLL. In this I want to add a member of a keypad class I made. But I would like that EVERY CEdit derived class will only use ONE copy of this keypad. Can this be done just by declaring the keypad as a static member. Then when something it is entered into the keypad, it will be sent to the perspective Cedit class. Can someone help me shed some light on this? Thanks ahead of time. Stan the man

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            There's only a couple of reasons I can think of to share a class this way: 1) You're sharing data between the various CEdit objects 2) There are a lot of your CEdit objects in existance at a time and you're concerned with memory usage, performance (the time to create the keypad object), or memory fragmentation. I try to avoid static members unless the class is intended to provide static methods. If it were me, and if item 2 is the issue, I would dynamically create a single application global instance of the keypad and just use it wherever it's needed.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            S 1 Reply Last reply
            0
            • realJSOPR realJSOP

              There's only a couple of reasons I can think of to share a class this way: 1) You're sharing data between the various CEdit objects 2) There are a lot of your CEdit objects in existance at a time and you're concerned with memory usage, performance (the time to create the keypad object), or memory fragmentation. I try to avoid static members unless the class is intended to provide static methods. If it were me, and if item 2 is the issue, I would dynamically create a single application global instance of the keypad and just use it wherever it's needed.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              S Offline
              S Offline
              Stan the man
              wrote on last edited by
              #6

              Actually, I do have lots of Edits. But I wanted to have a simple way to just declare each edit and then have the keypad function all built in without additionally allocating it again globally and then tie it to each Edit somehow. Thanks for all the responses and comments!

              realJSOPR 1 Reply Last reply
              0
              • S Stan the man

                Actually, I do have lots of Edits. But I wanted to have a simple way to just declare each edit and then have the keypad function all built in without additionally allocating it again globally and then tie it to each Edit somehow. Thanks for all the responses and comments!

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                Well, once you allocate your CEdit iobject on the heap, everything inside it also resides on the heap, so you can declare your keybad object like so: class CMyEdit { private: CMyKedPad m_KeyPad; }; ...and then initialize it in the CMyEdit constructor. At that point, there's no overhead involved in allocating that object on the heap, and you don't have to worry about delete/null it when your CMyEdit object cleans itself up. My guess is that your KeyPad object doesn't contain much data, so it's not like multiple instances is going to take up much space (probably less than 64 bytes). I still see no need for a static object (which is my entire point).

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                S 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  Well, once you allocate your CEdit iobject on the heap, everything inside it also resides on the heap, so you can declare your keybad object like so: class CMyEdit { private: CMyKedPad m_KeyPad; }; ...and then initialize it in the CMyEdit constructor. At that point, there's no overhead involved in allocating that object on the heap, and you don't have to worry about delete/null it when your CMyEdit object cleans itself up. My guess is that your KeyPad object doesn't contain much data, so it's not like multiple instances is going to take up much space (probably less than 64 bytes). I still see no need for a static object (which is my entire point).

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  S Offline
                  S Offline
                  Stan the man
                  wrote on last edited by
                  #8

                  Well, the keypad will be a dialog of sort (enable touchscreen capability). In this case, would you still think a static is not necessary and then just declare the kaypad class in the Edit class? Thanks. Stan

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups