Click here to Skip to main content
12,509,612 members (47,510 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VC6 C++ MFC
Can I use-
CDialog *dlg;
dlg->DoModal();
to display dialog box?
Posted 28-Jan-13 0:29am
Updated 28-Jan-13 0:35am
Jochen Arndt142.8K
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can't use it that way (your program will crash). You may use it this way:

// Allocate CDialog object on the heap.
// Object must be destroyed when no longer needed.
CDialog *pDlg = new CDialog;
pDlg->DoModal();
delete pDlg;
 
// Allocate CDialog object on the stack.
// Object is destroyed when going out of scope.
CDialog dlg;
dlg.DoModal();
  Permalink  
Comments
virus131 28-Jan-13 6:44am
   
Ok...... Thank you very much for your reply....
I want to know one more thing, can I use-
CDilaog *pDlg;
pDlg->Create();
Jochen Arndt 28-Jan-13 6:57am
   
No. When using 'CDialog *dlg', you create a pointer to a CDialog object but not the object itself. When using the new operator from my first example, the pointer is initialized with a newly created instance of a CDialog object.

The Create() function does not create the CDialog object, but the dialog so that it can be shown on the screen.
virus131 28-Jan-13 7:03am
   
ok.......... thanks a lot......
H.Brydon 30-Jan-13 2:12am
   
+5 ... close to what I would have said.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160929.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100