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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Which is The Best Way To Do It ???

Which is The Best Way To Do It ???

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 10 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.
  • J Jon Hulatt

    Hi. I have two classes, CMyFirstClass and CMySecondClass. MyFirstClass creates CMySecondClass objects dynamically. It is NEVER appropriate to create a CMySecondClass outside of the CMyFirstClass object. To make things tidier, i put the declaration of CMySecondClass inside of CMyFirstClass. Now I have a shorter list in Class View. And my app still works. My question: Is this a good idea? is there a compelling reason NOT to nest class declarations? Jon Sorry to dissapoint you all with my lack of a witty or poignant signature.

    B Offline
    B Offline
    Braulio Dez
    wrote on last edited by
    #2

    Hi, Nested classes are quite heavy used in MFC, If I´m not wrong ( or if the things haven´t change this times :-) ), for example is how MFC creates COM objects ( the macros for the interfaces, declare just nested classes), so I think it´s ok. HTH Braulio

    1 Reply Last reply
    0
    • J Jon Hulatt

      Hi. I have two classes, CMyFirstClass and CMySecondClass. MyFirstClass creates CMySecondClass objects dynamically. It is NEVER appropriate to create a CMySecondClass outside of the CMyFirstClass object. To make things tidier, i put the declaration of CMySecondClass inside of CMyFirstClass. Now I have a shorter list in Class View. And my app still works. My question: Is this a good idea? is there a compelling reason NOT to nest class declarations? Jon Sorry to dissapoint you all with my lack of a witty or poignant signature.

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #3

      Jon Hulatt wrote: Is this a good idea? why not? Mazy "So,so you think you can tell, Heaven from Hell, Blue skies from pain,... How I wish,how I wish you were here."
      Wish You Were Here-Pink Floyd-1975

      1 Reply Last reply
      0
      • J Jon Hulatt

        Hi. I have two classes, CMyFirstClass and CMySecondClass. MyFirstClass creates CMySecondClass objects dynamically. It is NEVER appropriate to create a CMySecondClass outside of the CMyFirstClass object. To make things tidier, i put the declaration of CMySecondClass inside of CMyFirstClass. Now I have a shorter list in Class View. And my app still works. My question: Is this a good idea? is there a compelling reason NOT to nest class declarations? Jon Sorry to dissapoint you all with my lack of a witty or poignant signature.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #4

        I think it is more a style thing. Unless it is a very small class utility class I don't like to nest classes. Makes it harder to read otherwise. Just my opinion though.

        1 Reply Last reply
        0
        • J Jon Hulatt

          Hi. I have two classes, CMyFirstClass and CMySecondClass. MyFirstClass creates CMySecondClass objects dynamically. It is NEVER appropriate to create a CMySecondClass outside of the CMyFirstClass object. To make things tidier, i put the declaration of CMySecondClass inside of CMyFirstClass. Now I have a shorter list in Class View. And my app still works. My question: Is this a good idea? is there a compelling reason NOT to nest class declarations? Jon Sorry to dissapoint you all with my lack of a witty or poignant signature.

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #5

          You don't prevent third code from creating CMySecondCLasses just by defining it into CMyFirstClass. It is required also that:

          1. The nested class lies in the private part of the nesting class,
          2. or the constructors for the nested class are private and the nesting class is a friend of the nested class

          Apart from this, nesting it is an excellent way to restrict visiblity and show design dependencies between classes (IMHO). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          J 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            You don't prevent third code from creating CMySecondCLasses just by defining it into CMyFirstClass. It is required also that:

            1. The nested class lies in the private part of the nesting class,
            2. or the constructors for the nested class are private and the nesting class is a friend of the nested class

            Apart from this, nesting it is an excellent way to restrict visiblity and show design dependencies between classes (IMHO). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            J Offline
            J Offline
            Jon Hulatt
            wrote on last edited by
            #6

            The declaration of the second class is within a protected: section. Doesn't really bother me if technically i could create an instance of the second class elsewhere, it's definately more of a design and organisation thing. I just wondered if i was overlooking some limitation or side effect of class nesting. Obviously not, great! Sorry to dissapoint you all with my lack of a witty or poignant signature.

            N 1 Reply Last reply
            0
            • J Jon Hulatt

              The declaration of the second class is within a protected: section. Doesn't really bother me if technically i could create an instance of the second class elsewhere, it's definately more of a design and organisation thing. I just wondered if i was overlooking some limitation or side effect of class nesting. Obviously not, great! Sorry to dissapoint you all with my lack of a witty or poignant signature.

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #7

              I do a good deal of nesting, though I nest structs within classes rather than classes within classes :-) Nish My most recent CP article :- A newbie's elementary guide to spawning processes www.busterboy.org

              J 1 Reply Last reply
              0
              • N Nish Nishant

                I do a good deal of nesting, though I nest structs within classes rather than classes within classes :-) Nish My most recent CP article :- A newbie's elementary guide to spawning processes www.busterboy.org

                J Offline
                J Offline
                Jon Hulatt
                wrote on last edited by
                #8

                thanks nish. although it's got to be a class, because it has member functions and things. besides, what's the difference between a struct anyway? And I like the whole nested classes thing. it makes things more confusing for the uninitiated. Which makes me feel cleverer. Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? ;-) Sorry to dissapoint you all with my lack of a witty or poignant signature.

                T P RaviBeeR 4 Replies Last reply
                0
                • J Jon Hulatt

                  thanks nish. although it's got to be a class, because it has member functions and things. besides, what's the difference between a struct anyway? And I like the whole nested classes thing. it makes things more confusing for the uninitiated. Which makes me feel cleverer. Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? ;-) Sorry to dissapoint you all with my lack of a witty or poignant signature.

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #9

                  Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? :laugh: No, because every time I do something like that, I am the one who can't make heads or tails out of it 2 months later. :laugh: Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                  1 Reply Last reply
                  0
                  • J Jon Hulatt

                    thanks nish. although it's got to be a class, because it has member functions and things. besides, what's the difference between a struct anyway? And I like the whole nested classes thing. it makes things more confusing for the uninitiated. Which makes me feel cleverer. Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? ;-) Sorry to dissapoint you all with my lack of a witty or poignant signature.

                    P Offline
                    P Offline
                    Paul M Watt
                    wrote on last edited by
                    #10

                    A struct and a class are basically the same thing in c++, however a struct defaults all of its members to public and the classes default declaration is private. Other than that you can inherit and do all of the things that you can do from a class, with a struct.

                    1 Reply Last reply
                    0
                    • J Jon Hulatt

                      thanks nish. although it's got to be a class, because it has member functions and things. besides, what's the difference between a struct anyway? And I like the whole nested classes thing. it makes things more confusing for the uninitiated. Which makes me feel cleverer. Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? ;-) Sorry to dissapoint you all with my lack of a witty or poignant signature.

                      P Offline
                      P Offline
                      Paul M Watt
                      wrote on last edited by
                      #11

                      Jon Hulatt wrote: Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? I enjoy it, but I also enjoy trying to explain to someone what I actually did, and try to help them understand it, then they get a deeper appreciation for some of the code that I develop.

                      1 Reply Last reply
                      0
                      • J Jon Hulatt

                        thanks nish. although it's got to be a class, because it has member functions and things. besides, what's the difference between a struct anyway? And I like the whole nested classes thing. it makes things more confusing for the uninitiated. Which makes me feel cleverer. Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? ;-) Sorry to dissapoint you all with my lack of a witty or poignant signature.

                        RaviBeeR Offline
                        RaviBeeR Offline
                        RaviBee
                        wrote on last edited by
                        #12

                        Jon Hulatt wrote: Does anyone else here secretly enjoy it when they come up with something really complicated that noone else will ever make head or tail of? I prefer hearing that my code and interfaces are clear and easy to understand. Code that is extremely hard to understand is usually an indication of poor design. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

                        1 Reply Last reply
                        0
                        • J Jon Hulatt

                          Hi. I have two classes, CMyFirstClass and CMySecondClass. MyFirstClass creates CMySecondClass objects dynamically. It is NEVER appropriate to create a CMySecondClass outside of the CMyFirstClass object. To make things tidier, i put the declaration of CMySecondClass inside of CMyFirstClass. Now I have a shorter list in Class View. And my app still works. My question: Is this a good idea? is there a compelling reason NOT to nest class declarations? Jon Sorry to dissapoint you all with my lack of a witty or poignant signature.

                          S Offline
                          S Offline
                          Stephen C Steel
                          wrote on last edited by
                          #13

                          Theres nothing wrong with nested classes. If CMySecondClass is only meaningful in the context of a CMyFirstClass object, then nesting the classes makes this clear.

                          However, if the second class is an implementation detail that doesn't appear in the public interface, there is another technique that you may be able to apply. In the header file that declares CMyFirstClass you simply give a forward reference to CMySecondClass:

                          class CMySecondClass; // forward declaration

                          class CMyFirstClass
                          {
                          public:
                          // public interface (CMySecondClass not mentioned)

                          protected:
                          CMySecondClass *m_p_instance;

                          //protected or private member functions
                          //which may include arguments of type
                          //(CMySecondClass *) or (CMySecondClass&)
                          };

                          Now, both the declaration and implementation of CMySecondClass can be put in the implementation file for CMyFirstClass. This provides much stronger encapsulation than a nested protected or private class, since the details of CMySecondClass aren't even visible to code that uses CMyFirstClass (other than the fact that CMySecondClass exists). With a nested protected or private class, the details of CMySecondClass are visible, but access is restricted. Stephen C. Steel Kerr Vayne Systems Ltd.

                          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