Baldewin
Baldewin

Reputation: 1653

Where to call DestroyWindow() of an MFC dialog?

I'm curently working on dialogs in an MFC application and I'm – admittedly – quite new to MFC.

Let's say I have class A (derived from CDialog) that uses class B (also derived from CDialog). Thus, A::OnInitDialog() calls the create(...) method of B.

I saw now that the destructor of class B contains

if ( GetSafeHwnd() )
{
    DestroyWindow();
}

Is this okay? In my understanding it would be better to call B's DestroyWindow()method in A::OnDestroy(). Is that right?

Thanks for your help!
Oliver

Upvotes: 3

Views: 4220

Answers (1)

Moo-Juice
Moo-Juice

Reputation: 38820

One thing you may have noticed as you've delved in to MFC is that it is a wrapper API and not strictly object-orientated. Whereas we would like to use RAII (Resource Acquisition Is Initialisation), MFC does not create windows in its constructor. It does it, as you rightly point out, through the Create() method.

Therefore it makes more sense to me, given the way MFC works, To destroy B when A is being destroy (A::OnDestroy), so I think you're going down the right path.

Upvotes: 3

Related Questions