Share via


Overriding a Virtual Function

OverviewHow Do I

Note   Virtual functions can not be added to an ATL COM object using ClassView, WizardBar, or ClassWizard.

You can use ClassView, WizardBar, or ClassWizard to override virtual functions defined in a base class.

To override a virtual function with ClassView or WizardBar

  1. From ClassView, right-click on a class and click Add Virtual Function on the shortcut menu.

    -or-

    Using WizardBar's Class drop-down list, select a class, then on the Action menu click Add Virtual Function.

  2. The New Virtual Override dialog box appears, prompting you with a list of virtual functions to add to the selected class. If you click Add Handler, the dialog adds a stub function to the class's implementation file, for example:

    // CChildFrame message handlers
    void CChildFrame::ActivateFrame(int nCmdShow)
    {
        // TODO: Add your specialized code here
        // or call the base class
        CMDIChildWnd::ActivateFrame(nCmdShow);
    }
    

    If you want to edit the virtual function you are adding, click Add and Edit; this navigates to the file in which the new virtual function is implemented.

  3. If you want to edit an existing virtual function, select an item from Existing virtual function overrides, and click Edit Existing to navigate to the file in which the virtual function is implemented.

To override a virtual function with ClassWizard

  1. On the View menu, click ClassWizard.

    ClassWizard appears.

  2. On the Message Maps tab, under Class name, select the name of the class in which you want to override a virtual function.

  3. In the Object IDs box, select the class name.

    The Messages box displays a list of virtual functions you can override and a list of Windows messages. The virtuals come before the messages and appear in mixed case.

  4. In the Messages box, select the name of the virtual function you want to override.

  5. Click Add Function.

    The function is created and its name displayed in the Member Functions box. The names of virtual overrides are preceded by a gray glyph containing the letter "V" (handlers have a "W").

  6. Click Edit Code to jump to the virtual function definition.

See Also   Adding a Class, Adding a Member Function, Adding a Member Variable, Adding a Message Handler, Deleting a Member Function, Navigating the Class Structure