Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Can I use-
C++
CDialog *dlg;
dlg->DoModal();

to display dialog box?
Posted
Updated 28-Jan-13 0:35am
v2

1 solution

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

C++
// 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();
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900