Click to See Complete Forum and Search --> : Basic question about constructors and initializers in VC++
Mahesh Chari
May 10th, 1999, 11:07 AM
Hi,
Why does Visual C++ provide the OnInitDialog function when the dialog already has a constructor , i.e, can I not perform all initializations for a dialog in the constructor of the dialog itself (instead of in the OnInitDialog function) ?
Thanks in advance.
mc
PChetan
May 10th, 1999, 12:51 PM
Hi,
When an instance of your dialog class is created and its constructor is called, the actual dialog is not created so in the constructor, the common controls in your dialog are not created, if you will try to access those controls you will get well known GPF buddy. But when the function OnInitDialog gets called, the dialog is created i.e. all controls on your dialog box are created and that's why onInitDialog is the safest place to initialize controls on the dialog.
Yes, if you have some member veriables in your dialog class then definitely you can initialize those variables in the constructor.
I hope this answer will give you an answer.
Thanks
Chetan
Nothing is imposible.
Paul McKenzie
May 10th, 1999, 04:46 PM
Simple. The constructor is nothing more than straight C++ and for the most part has nothing to do with a Windows dialog, or even Windows. It allows you to add any initialization code before the dialog is created by Windows.
However, the OnInitDialog() is actually invoked by MFC when the operating system (Windows) sends a message (WM_INITDIALOG) to the dialog's window procedure. The only things that you can't do in the constructor is manipulating the dialog's controls, since they do not exist yet.
Windows knows *nothing* about C++ constructors; if it did, C, VB, Delphi, PowerBuilder, etc. programmers would not be able to write Windows programs!
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.