What does it mean to set the declaration of a function equal to 0? How can you assign an integer to a function?

23,308

Solution 1

That line of code defines a pure virtual function in C++. It has nothing to do with the otherwise tricky Win32 API or GUI code in general.

A pure virtual function is a virtual function that is used when the designer of the class wants to force derived classes to override the function and provide their own implementation.

If a class contains any pure virtual functions, it is considered an "abstract" class and instances of that class cannot be instantiated.

C++ uses the special syntax = 0; to indicate pure virtual functions instead of adding a new keyword to the language (as languages like C# do). You can think of it as setting the function pointer to 0.

Also see the answers to this related question: What are the uses of pure virtual functions in C++?


(By the way, the Windows header files <windows.h> simply define NULL as 0. So the programmer technically could have written = NULL, but it's much clearer to use the numeric constant 0 and reserve NULL for pointer values.)

Solution 2

It is a pure virtual function.

The =0 is just the syntax used to indicate that it is a pure virtual function.

Presence of a Pure virtual function in a class makes the class an Abstract class. One cannot create an object of any abstract class. Although, a pointer or reference to such an Abstract class can be created. Your derived class needs to override that method.

What is the purpose of making a function pure virtual?

Usually, A function is made pure virtual so that the designer of the Abstract Base class can enforce the derived class to override that function and provide it's own implementation. Important to note though, that a pure virtual function can have a implementation of its own, so the derived class can call Base class version of the function.

Sometimes a pure virtual function is added just to make the base class Abstract (so it's instances cannot be created). Usually, in such a situation instead of adding a dummy function to make a class Abstract the destructor of the class is made pure virtual.

Solution 3

This is a "pure virtual" function that you need to override in a derived class.

Share:
23,308
ApprenticeHacker
Author by

ApprenticeHacker

I try to make the internet faster.

Updated on July 08, 2022

Comments

  • ApprenticeHacker
    ApprenticeHacker almost 2 years

    I was browsing through the sources of a (prefer not to name) GUI Toolkit which wrapped up the Windows API when I found the following function definition in the window class:

     virtual LRESULT CALLBACK wndProc (HWND, UINT, WPARAM, LPARAM) = 0;
    

    What is happening here? How can you assign a function to an integer? Or does it assign it to NULL? Do you need to do this if you want to use function pointers in the wndproc?

  • ApprenticeHacker
    ApprenticeHacker almost 13 years
    I understand. The win api is so complex its impossible to know when it's just pure c++ or some api macro sometimes
  • Cody Gray
    Cody Gray almost 13 years
    @burningprodigy: Definitely an argument for learning C++ (the language) before you start trying to learn the Win32 API. I learned them together, but I probably wouldn't recommend that to anyone else. :-) My knowledge of C++ definitely suffers/suffered because of it.