Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 717
ApprenticeHacker Avatar asked Jul 09 '11 11:07

ApprenticeHacker


People also ask

How do you set a zero to a function?

Zeros of a function definitionThe zeros of a function are the values of x when f(x) is equal to 0. Hence, its name. This means that when f(x) = 0, x is a zero of the function. When the graph passes through x = a, a is said to be a zero of the function.

What is function declaration of function?

Function Declarations A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration has the following parts − return_type function_name( parameter list );

How do you write a function declaration?

A function declaration is made of function keyword, followed by an obligatory function name, a list of parameters in a pair of parenthesis (para1, ..., paramN) and a pair of curly braces {...} that delimits the body code.

What is function declaration with example?

The first declares a function f that takes two integer arguments and has a return type of void : void f(int, int); This fragment declares a pointer p1 to a function that takes a pointer to a constant character and returns an integer: int (*p1) (const char*);


2 Answers

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.)

like image 99
Cody Gray Avatar answered Sep 30 '22 22:09

Cody Gray


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.

like image 44
Alok Save Avatar answered Sep 30 '22 22:09

Alok Save