Newbie Q - Pointer vs None pointer references.

This is a discussion on Newbie Q - Pointer vs None pointer references. within the Windows Programming forums, part of the Platform Specific Boards category; Ok, This has been bugging me for about a week now. It's sort of a C++ question but since it's ...

  1. #1
    Assembler + Basic from 79
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    22

    Newbie Q - Pointer vs None pointer references.

    Ok,
    This has been bugging me for about a week now. It's sort of a C++ question but since it's in the context of MFC, I figured I'd drop it here.

    Now I know the '->' operator is for referencing members and member functions of a class or struct referenced by a pointer. I know the '.' is used to reference members and member functions of a class or struct represented by an object as aposed to a pointer.

    Now that may have been confusing but the code I have here should clear it up. The book I'm studying on MFC has described accessing the same types of objects in BOTH ways in differing pieces of code but never explains why he chose to access it in that way.

    Code:
        CDialog dlg(IDD_ABOUTDIALOG, this);
        dlg.DoModal();
    And now this way...
    Code:
        CDialog* dlg = new CDialog(IDD_ABOUTDIALOG, this);
        dlg->DoModal();
        delete dlg;
    Now I know in the second instance the dialog was created dynamically via the 'new' keyword, but what I don't understand is why he made dlg a pointer rather then an object? He's also done this with CPaintDC as well (not to mention CPen, CBrush and a few others) and I can't get a handle on the reason behind the variations.

    Any help would sure be appreaciated.

    P.S. C++ and MFC are both new to me (2 months now) Aside from the dumb question, does it sound like I'm getting a handle on this stuff or does it sound like I'm still completely lost?
    Twin engine aircraft are way better. If one engine quits the other takes you to the scene of the crash.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,667
    Well, assuming you will be discarding the object referenced by the pointer, then that would be most advantageous. Barring that, I think it's simply a matter of preference...



    ITSA
    Socket Library!

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    8
    Basically, anything you put on the freestack with the new operator is permanent until you delete it. This gives you the option of controling your own memory allocation. Concerning the dialog box there is a main difference in that when you do the DoModal(), the dialog box will stay with the focus until you release it. With the pointer, new and Create() you can change the focus off of the dialog box to another window.

    When you use pointers with CBrush, CPEN, etc you can create it once and use these over and over by simply referencing the address(&) to the pointer. Secondly by doing this you will only be passing a 4bit pointer instead of an entire class or struct if you send it to a function. This is why you send pointers referencing class's like CPaintDC, CClientDC or just the CDC class instead of the entire class.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    This probably has something to do with The modeless option of displaying Dialogs. If they're not created dynamically, they'll only appear for a second.. due to the variable going out of scope and destroying the object.

  5. #5
    Assembler + Basic from 79
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    22
    digital dropout,
    The book did speak about the difference between creating them locally on the heap through the first method whereby it's supposed to destroy itself as soon as it goes out of scope, and creating it manually via the second method where you need to manually delete the object yourself.

    You raised a good point regarding moving a pointer around rather then the entire class or struct. I hadn't thought of that one at all.

    The bug up my butt with this author is the variation from code sample to code sample for no apparent reason. In the last sample he created the dialog as an object rather then a pointer only to use it for one dialog box.

    Duel-Catfish,
    I'm not sure what your refering to. I've not seen anything of the sort. But then again, everything I've been doing is Modal.
    Twin engine aircraft are way better. If one engine quits the other takes you to the scene of the crash.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    When you init dialogs with the Create() or create() member function (I can't remember) they're created modeless, meaning, you can switch back to your program and the dialog freely. With modal, you must dismiss the dialog and return a value to the program before you can switch back to the actual program. If you use create() on the stack, the variable will go out of scope as soon as the calling function exits, and your modeless dialog will be nothing more then a flicker on the screen.

  7. #7
    Assembler + Basic from 79
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    22
    Dual-Catfish,
    OK, Now I get ya.. Something akin to setting dialog visibaility to false.. except the modeless one is actually "outa there"
    Twin engine aircraft are way better. If one engine quits the other takes you to the scene of the crash.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Newbie Pointer question
    By kook44 in forum C++ Programming
    Replies: 5
    Last Post: 01-02-2006, 12:07 PM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 08:40 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Newbie char pointer problem
    By larry in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2001, 09:38 AM

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