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. OOP is driving me crazy!

OOP is driving me crazy!

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++question
26 Posts 6 Posters 2 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.
  • L Lord Kixdemp

    Hello everyone! I've been coding in C for a long time. Now I' starting to get into C++. Well, my last 2 projects which I started in C++ I had to convert to C because I couldn't figure out how to use classes. Take for example my current situation: There is a Dialog class, which holds all of the GUI functionality of my program. The Dialog class contains an object of type Client, which holds all of the networking functionality. Well, Client wants to interact with a GUI control inside Dialog. But that's not possible because C++ doesn't allow Client to know anything about Dialog. So how would I solve that? If you can answer, I will thank you for life. :) Thanks in advance!

    Windows Calculator told me I will die at 28. :(

    L Offline
    L Offline
    Lord Kixdemp
    wrote on last edited by
    #6

    I deserve harsh punishment for not Googling first. :mad:

    1 Reply Last reply
    0
    • L Lord Kixdemp

      Hello everyone! I've been coding in C for a long time. Now I' starting to get into C++. Well, my last 2 projects which I started in C++ I had to convert to C because I couldn't figure out how to use classes. Take for example my current situation: There is a Dialog class, which holds all of the GUI functionality of my program. The Dialog class contains an object of type Client, which holds all of the networking functionality. Well, Client wants to interact with a GUI control inside Dialog. But that's not possible because C++ doesn't allow Client to know anything about Dialog. So how would I solve that? If you can answer, I will thank you for life. :) Thanks in advance!

      Windows Calculator told me I will die at 28. :(

      L Offline
      L Offline
      Lord Kixdemp
      wrote on last edited by
      #7

      I deserve harsh punishment for not Googling first. :mad: http://www.thescripts.com/forum/thread63131.html[^] Thanks all very much for your time!

      L 1 Reply Last reply
      0
      • L Lord Kixdemp

        Yes, but Dialog needs Client too... They both need each other, and with the include guards that's not possible. And if I remove the include guards I get infinite error messages by the compiler... :(( Any more ideas? Thanks!

        Windows Calculator told me I will die at 28. :(

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #8

        You can do stuff like this... //----------------------------- //Dialog.h //forward declaration class CClient; /*Dialog can decalre pointers to CClient only and no code that uses them in the header file*/ class CDialog { public: CDialog(CClient* pClient); private: CClient* m_pClient; }; //----------------------------- //Client.h #include "Dialog.h" class CClient { //Client can use anything from Dialog CClient(CDialog& aDialog) : m_Dialog(aDialog) { } private: CDialog& m_Dialog }; //----------------------------- //Dialog.cpp #include "Client.h"//This inclues Dialog.h as well //Now you can write Dialog code that does stuff with Clients :-) CDialog::CDialog(CClient* pClient) { m_pClient = pClient; }

        Nothing is exactly what it seems but everything with seems can be unpicked.

        L 1 Reply Last reply
        0
        • L Lord Kixdemp

          Yes, but Dialog needs Client too... They both need each other, and with the include guards that's not possible. And if I remove the include guards I get infinite error messages by the compiler... :(( Any more ideas? Thanks!

          Windows Calculator told me I will die at 28. :(

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #9

          Well, this is not a big problem. For instance, you can use pointers instead of references and avoid including headers inside each other exploiting class deferred declaration:

          // this is B.h

          #pragma once
          class A; // class a deferred declaration
          class B
          {
          A * _pA;
          public:
          B(A * pA):_pA(pA){}
          public:
          ~B(void){};
          };

          Note I've not included A.h in the class B header. Of course you have to include A.h inside B.cpp to do something useful with the _pA pointer, but this will be not a problem anymore. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • L Lord Kixdemp

            Hello everyone! I've been coding in C for a long time. Now I' starting to get into C++. Well, my last 2 projects which I started in C++ I had to convert to C because I couldn't figure out how to use classes. Take for example my current situation: There is a Dialog class, which holds all of the GUI functionality of my program. The Dialog class contains an object of type Client, which holds all of the networking functionality. Well, Client wants to interact with a GUI control inside Dialog. But that's not possible because C++ doesn't allow Client to know anything about Dialog. So how would I solve that? If you can answer, I will thank you for life. :) Thanks in advance!

            Windows Calculator told me I will die at 28. :(

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #10

            One possible solution is to send messages to the dialog. Your client needs the target Window (CWnd*) and you can use the SendMessage() , or I suggest the PostMessage() API because it is non blocking. Another way are global objects and structures or callbacks. Interesting and important is to use the Timer-function to execute regurarly some actions!!! Search for WM_TIMER

            Greetings from Germany

            1 Reply Last reply
            0
            • CPalliniC CPallini

              Well, one way is the following: - Change Client class constructor to accept a Dialog class reference. Store the passed reference as member of Client class - Make Dialog class public methods to allow access to relevant class data. (a brutal alternative to this step is declaring Client class friend of Dialog class). For instance: // Client

              class Client
              {
              Dialog & m_dlg;
              public:
              Client( Dialog & dialog):m_dlg(dialog){}
              ...
              }

              // Dialog
              class Dialog
              {
              public:
              setLabelText(LPCTSTR szText)
              {
              ...
              }
              ...
              }

              and then, somewhere in your code:

              Dialog dlg;
              ...
              Client client(dlg);
              ...

              and, finally, somewhere in Client code:

              m_dlg.setLabelText(_T("packet received"));

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #11

              Really, that's your advice? Are you kidding me! :omg: :wtf:

              CPalliniC 1 Reply Last reply
              0
              • L led mike

                Really, that's your advice? Are you kidding me! :omg: :wtf:

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #12

                led mike wrote:

                Really, that's your advice?

                Yes.

                led mike wrote:

                Are you kidding me!

                Why do you think that? :) OK you're right, in my proposal there is (almost ;P ) no way to avoid the troublesome interdependency of the headers. :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                modified on Tuesday, December 11, 2007 11:12:43 AM

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • L Lord Kixdemp

                  I deserve harsh punishment for not Googling first. :mad: http://www.thescripts.com/forum/thread63131.html[^] Thanks all very much for your time!

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #13

                  Lord Kixdemp wrote:

                  I deserve harsh punishment for not Googling first.

                  And you will likely receive harsh punishment if you tightly couple a business object to a view. Your punishment will be in the form of dealing with the technical debt[^] of a poor design. OOD is a tool that can solve problems of software development like hard to maintain and inflexible code. Notice I said it "can" solve those problems, or a sub optimal design, like one that would tightly couple business classes to view classes, could result in "creating" those same problems. There is much information available on the subject of Object Oriented Design. I strongly suggest you take some time to become familiar with things like best practices and principles of object oriented design. You might try some articles or tutorials and for sure get introduced to Software Design Patterns[^]. Good luck

                  L 1 Reply Last reply
                  0
                  • M Matthew Faithfull

                    You can do stuff like this... //----------------------------- //Dialog.h //forward declaration class CClient; /*Dialog can decalre pointers to CClient only and no code that uses them in the header file*/ class CDialog { public: CDialog(CClient* pClient); private: CClient* m_pClient; }; //----------------------------- //Client.h #include "Dialog.h" class CClient { //Client can use anything from Dialog CClient(CDialog& aDialog) : m_Dialog(aDialog) { } private: CDialog& m_Dialog }; //----------------------------- //Dialog.cpp #include "Client.h"//This inclues Dialog.h as well //Now you can write Dialog code that does stuff with Clients :-) CDialog::CDialog(CClient* pClient) { m_pClient = pClient; }

                    Nothing is exactly what it seems but everything with seems can be unpicked.

                    L Offline
                    L Offline
                    led mike
                    wrote on last edited by
                    #14

                    Matthew Faithfull wrote:

                    You can do stuff like this...

                    Yeah I could also create a function that takes as a parameter a reference to a pointer to a pointer of a structure that contains a union that has a member that is a pointer to a pointer to a structure that contains a member that is a pointer to a function that takes as a parameter.... Not everything you can do is something you should do. Take a clue from best practices and principles of object oriented design and software design patterns. Or don't .... whatever, but try not to give bad advice to beginners that may still have a hope of becoming capable of creating something other than technical debt.

                    M CPalliniC 2 Replies Last reply
                    0
                    • L led mike

                      Matthew Faithfull wrote:

                      You can do stuff like this...

                      Yeah I could also create a function that takes as a parameter a reference to a pointer to a pointer of a structure that contains a union that has a member that is a pointer to a pointer to a structure that contains a member that is a pointer to a function that takes as a parameter.... Not everything you can do is something you should do. Take a clue from best practices and principles of object oriented design and software design patterns. Or don't .... whatever, but try not to give bad advice to beginners that may still have a hope of becoming capable of creating something other than technical debt.

                      M Offline
                      M Offline
                      Matthew Faithfull
                      wrote on last edited by
                      #15

                      Or you could take your own advice :) The technique I described of forward declaring a related class and using only the declaration in the header so that both classes can 'know' about each other is both standard, as in used everywhere, and extremely useful as in you're pretty stuffed wihtout it. I don't know or care what the OPs objects do I only know about the interface he's given me, the question. by all means post a better solution, more appropriate to a beginner, easy to understand, more general, more useful and wiser in every way. I'd certainly suggest doing so before criticising the perfectly reasonable advice already posted or no one including the OP is likely to take any notice.:|

                      Nothing is exactly what it seems but everything with seems can be unpicked.

                      L 1 Reply Last reply
                      0
                      • M Matthew Faithfull

                        Or you could take your own advice :) The technique I described of forward declaring a related class and using only the declaration in the header so that both classes can 'know' about each other is both standard, as in used everywhere, and extremely useful as in you're pretty stuffed wihtout it. I don't know or care what the OPs objects do I only know about the interface he's given me, the question. by all means post a better solution, more appropriate to a beginner, easy to understand, more general, more useful and wiser in every way. I'd certainly suggest doing so before criticising the perfectly reasonable advice already posted or no one including the OP is likely to take any notice.:|

                        Nothing is exactly what it seems but everything with seems can be unpicked.

                        L Offline
                        L Offline
                        led mike
                        wrote on last edited by
                        #16

                        Matthew Faithfull wrote:

                        by all means post a better solution

                        I already did before I replied to your post.

                        Matthew Faithfull wrote:

                        Or you could take your own advice

                        If you were at all familiar with my posts you would not have made that statement. I am one of the few people on the site that attempt to introduce people to the benefits of object oriented design.

                        Matthew Faithfull wrote:

                        I don't know or care what the OPs objects do

                        You really didn't need to say that, it was very clear from your original post. I now return you to your regularly scheduled activity of manufacturing technical debt.

                        M 1 Reply Last reply
                        0
                        • L led mike

                          Matthew Faithfull wrote:

                          by all means post a better solution

                          I already did before I replied to your post.

                          Matthew Faithfull wrote:

                          Or you could take your own advice

                          If you were at all familiar with my posts you would not have made that statement. I am one of the few people on the site that attempt to introduce people to the benefits of object oriented design.

                          Matthew Faithfull wrote:

                          I don't know or care what the OPs objects do

                          You really didn't need to say that, it was very clear from your original post. I now return you to your regularly scheduled activity of manufacturing technical debt.

                          M Offline
                          M Offline
                          Matthew Faithfull
                          wrote on last edited by
                          #17

                          led mike wrote:

                          I already did before I replied to your post.

                          :confused:Strangely it seems to be absent form the forum.

                          led mike wrote:

                          If you were at all familiar with my posts you would not have made that statement. I am one of the few people on the site that attempt to introduce people to the benefits of object oriented design.

                          That wasn't the part of your advice I was refering to. I'm sure your OOP skills are second to none and you frequently point people in the right direction.

                          led mike wrote:

                          I now return you to your regularly scheduled activity of manufacturing technical debt.

                          :laugh: Don't assume I know less than you'd like me to just because we understand the OPs needs differently. I haven't manufactured any technical debt in years. :laugh:

                          Nothing is exactly what it seems but everything with seems can be unpicked.

                          L 1 Reply Last reply
                          0
                          • L led mike

                            Matthew Faithfull wrote:

                            You can do stuff like this...

                            Yeah I could also create a function that takes as a parameter a reference to a pointer to a pointer of a structure that contains a union that has a member that is a pointer to a pointer to a structure that contains a member that is a pointer to a function that takes as a parameter.... Not everything you can do is something you should do. Take a clue from best practices and principles of object oriented design and software design patterns. Or don't .... whatever, but try not to give bad advice to beginners that may still have a hope of becoming capable of creating something other than technical debt.

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #18

                            IMHO there is nothig bad in his proposed solution. :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                            In testa che avete, signor di Ceprano?

                            L 1 Reply Last reply
                            0
                            • M Matthew Faithfull

                              led mike wrote:

                              I already did before I replied to your post.

                              :confused:Strangely it seems to be absent form the forum.

                              led mike wrote:

                              If you were at all familiar with my posts you would not have made that statement. I am one of the few people on the site that attempt to introduce people to the benefits of object oriented design.

                              That wasn't the part of your advice I was refering to. I'm sure your OOP skills are second to none and you frequently point people in the right direction.

                              led mike wrote:

                              I now return you to your regularly scheduled activity of manufacturing technical debt.

                              :laugh: Don't assume I know less than you'd like me to just because we understand the OPs needs differently. I haven't manufactured any technical debt in years. :laugh:

                              Nothing is exactly what it seems but everything with seems can be unpicked.

                              L Offline
                              L Offline
                              led mike
                              wrote on last edited by
                              #19

                              Matthew Faithfull wrote:

                              Strangely it seems to be absent form the forum.

                              I can see it. I'm afraid to even try the Permalink feature on the "new and destroyed" version of CP! :-D

                              Matthew Faithfull wrote:

                              I haven't manufactured any technical debt in years.

                              So you haven't developed any software in years? It's all but impossible to keep from creating technical debt when developing software. It's just a matter of degrees and managing risk in most cases.

                              Matthew Faithfull wrote:

                              just because we understand the OPs needs differently.

                              Fair enough, I read the OP as being interested in the design aspects of his problem. It's very rare to see anyone posting something here that even hints at an interest in design issues.

                              M 1 Reply Last reply
                              0
                              • CPalliniC CPallini

                                IMHO there is nothig bad in his proposed solution. :)

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                L Offline
                                L Offline
                                led mike
                                wrote on last edited by
                                #20

                                Don't bother trying to convince me, just go right to someone like Kent Beck with your opinion.

                                CPalliniC 1 Reply Last reply
                                0
                                • L led mike

                                  Matthew Faithfull wrote:

                                  Strangely it seems to be absent form the forum.

                                  I can see it. I'm afraid to even try the Permalink feature on the "new and destroyed" version of CP! :-D

                                  Matthew Faithfull wrote:

                                  I haven't manufactured any technical debt in years.

                                  So you haven't developed any software in years? It's all but impossible to keep from creating technical debt when developing software. It's just a matter of degrees and managing risk in most cases.

                                  Matthew Faithfull wrote:

                                  just because we understand the OPs needs differently.

                                  Fair enough, I read the OP as being interested in the design aspects of his problem. It's very rare to see anyone posting something here that even hints at an interest in design issues.

                                  M Offline
                                  M Offline
                                  Matthew Faithfull
                                  wrote on last edited by
                                  #21

                                  led mike wrote:

                                  So you haven't developed any software in years?

                                  Sometimes it feels like it on this project but no, I reckon I've removed as much technical debt as I've added certainly in the last few years, 2 working on a great product where it's easy enough to add code with an absolute minimum of the usual risks. Before that the previous product I worked on was so bad that pretty much every change I made removed more problems that it created. Fair enough, I read the OP as being a C programmer doing nothing resembling actual OOP but wanting to use C++, to head in the right direction and needing a bridge from C to C++ thinking before diving in with patterns. I am quite familiar with the GOF and all their works. I've even posted a patterns article[^] which you're very welcome to shred if you so wish. :)

                                  Nothing is exactly what it seems but everything with seems can be unpicked.

                                  1 Reply Last reply
                                  0
                                  • L led mike

                                    Lord Kixdemp wrote:

                                    I deserve harsh punishment for not Googling first.

                                    And you will likely receive harsh punishment if you tightly couple a business object to a view. Your punishment will be in the form of dealing with the technical debt[^] of a poor design. OOD is a tool that can solve problems of software development like hard to maintain and inflexible code. Notice I said it "can" solve those problems, or a sub optimal design, like one that would tightly couple business classes to view classes, could result in "creating" those same problems. There is much information available on the subject of Object Oriented Design. I strongly suggest you take some time to become familiar with things like best practices and principles of object oriented design. You might try some articles or tutorials and for sure get introduced to Software Design Patterns[^]. Good luck

                                    L Offline
                                    L Offline
                                    Lord Kixdemp
                                    wrote on last edited by
                                    #22

                                    Wow, looks like there's more to software development than just coding. :wtf: I looked up for some tutorials, and found this bunch: http://www.devdaily.com/Dir/OOA_OOD/Patterns/Tutorials/[^] You seem to know about the subject, so would you mind pointing out which of those is the best to start with? :) Thanks!

                                    Windows Calculator told me I will die at 28. :(

                                    L 1 Reply Last reply
                                    0
                                    • L led mike

                                      Don't bother trying to convince me, just go right to someone like Kent Beck with your opinion.

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #23

                                      led mike wrote:

                                      Don't bother trying to convince me

                                      Oh, I don't need (and don't want) to do that. I just expressed my own opinion (maybe ad (dis)advantage ;) of other people reading the thread)

                                      led mike wrote:

                                      just go right to someone like Kent Beck with your opinion

                                      Good designs depend on a lot of factors, the less important being authority. :-D

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                      In testa che avete, signor di Ceprano?

                                      L 1 Reply Last reply
                                      0
                                      • CPalliniC CPallini

                                        led mike wrote:

                                        Don't bother trying to convince me

                                        Oh, I don't need (and don't want) to do that. I just expressed my own opinion (maybe ad (dis)advantage ;) of other people reading the thread)

                                        led mike wrote:

                                        just go right to someone like Kent Beck with your opinion

                                        Good designs depend on a lot of factors, the less important being authority. :-D

                                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                        L Offline
                                        L Offline
                                        led mike
                                        wrote on last edited by
                                        #24

                                        CPallini wrote:

                                        Good designs depend on a lot of factors, the less important being authority.

                                        I see, so you are someone that is so accomplished at software development that you do not need to heed the advice of well established experts in the industry. Ok well good luck with that. Happy Holidays and all. :rolleyes:

                                        CPalliniC 1 Reply Last reply
                                        0
                                        • L led mike

                                          CPallini wrote:

                                          Good designs depend on a lot of factors, the less important being authority.

                                          I see, so you are someone that is so accomplished at software development that you do not need to heed the advice of well established experts in the industry. Ok well good luck with that. Happy Holidays and all. :rolleyes:

                                          CPalliniC Offline
                                          CPalliniC Offline
                                          CPallini
                                          wrote on last edited by
                                          #25

                                          led mike wrote:

                                          I see, so you are someone that is so accomplished at software development that you do not need to heed the advice of well established experts in the industry.

                                          Of course I really need to heed their advice. This doesn't mean I'm not able to have an independent opinion.

                                          led mike wrote:

                                          Ok well good luck with that

                                          Thank you, I need it.

                                          led mike wrote:

                                          Happy Holidays and all.

                                          Happy Holidays to you! :)

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                          In testa che avete, signor di Ceprano?

                                          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