Figure 1 samp.cpp ////////////////////////////////////////////////////////////////
// Sample program illustrating 'hiding' problem when you override
// only one of a pair (or more) of overloaded virtual functions.
// This program generates a compiler error because the function
// test(void) in class D hides the function test(int) in class B.
//
#include <iostream.h>
class B {
private:
int nNumber;
public:
virtual void test() {
cout << "B::test()\n"; }
virtual void test(int x) {
nNumber = x; // use the param in some fashion
cout << "B::test(int x)\n";
}
};
class D : public B {
public:
//test(void) hides B::test(int)
virtual void test() {
cout << "D::test()\n";
}
// To fix the problem, remove comments below:
// virtual void test(int x) {
// B::test(x);
// }
};
void main(int argc, char* argv[])
{
D d; // derived class instance
d.test(); // OK
d.test(17); // generates compiler error.
}
Figure 2 dlgsamp ////////////////////////////////////////////////////////////////
// MSDN Magazine May 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// This fragment shows how to change the style of a control by
// destroying and recreating it. This technique is required for
// styles that can't be modified after window/control has been
// created. In this example, I add LBS_OWNERDRAWFIXED to a listbox.
//
//////////////////
// Some dialog with a listbox
//
class CMyDialog : public CDialog {
protected:
CListBox m_wndListBox;
virtual BOOL OnInitDialog();
•••
};
BOOL CMyDialog::OnInitDialog()
{
// First, subclass the listbox the normal way.
//
m_wndListBox.SubclassDlgItem(IDC_LIST1,this);
//
// Modify listbox style to add LBS_OWNERDRAWFIXED. Since
// Windows only looks at this style when the window is first
// created, you must destroy/recreate the listbox.
//
// Remember previous window for TAB order. (TAB order = Z order.)
//
CWnd *pWndPrev = m_wndListBox.GetWindow(GW_HWNDPREV);
// Remember state. If you need to save the list items, current
// selected item and so on, don't forget to do that too!
//
UINT nID = m_wndListBox.GetDlgCtrlID(); // remember ID..
DWORD dwStyle = m_wndListBox.GetStyle(); // style..
DWORD dwStyleEx = m_wndListBox.GetExStyle(); // extended style..
CRect rc; // and position
m_wndListBox.GetWindowRect(&rc); // ..
ScreenToClient(&rc); // (convert to client).
m_wndListBox.DestroyWindow(); // destroy listbox
dwStyle |= LBS_OWNERDRAWFIXED; // add new style
// Now recreate it!
//
m_wndListBox.CreateEx(dwStyleEx,
"ListBox", // (window class name)
NULL, // window text
dwStyle, // modified style
rc, // position
this, // parent window
nID); // control ID
// Finally, restore original Z-order, which is same as TAB
// order. This is important since otherwise the new listbox
// will go to the end of the TAB order.
//
m_wndListBox.SetWindowPos(pWndPrev,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
•••
return TRUE;
}
|