将函数的声明设置为0意味着什么?如何为函数分配一个整数? [英] What does it mean to set the declaration of a function equal to 0? How can you assign an integer to a function?
问题描述
当我在窗口类中找到以下函数定义时,我浏览了一个(不喜欢命名)GUI工具包的源代码,它包装了Windows API:
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;
这里发生了什么?如何将函数分配给整数?还是将它分配给 NULL
?
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?
推荐答案
如果你想在wndproc中使用函数指针,你需要这样做。 >纯虚函数。它与一般的Win32 API或GUI代码无关。
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.
如果一个类包含任何纯虚函数,它被认为是一个abstract / strong>,并且该类的实例无法实例化。
If a class contains any pure virtual functions, it is considered an "abstract" class and instances of that class cannot be instantiated.
C ++使用特殊语法 = 0;
纯虚函数,而不是向语言添加一个新的关键字(像C#do这样的语言)。您可以将其视为将函数指针设置为0。
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: Why would I want to use a pure virtual function in C++?
(顺便说一下,Windows头文件< windows.h
>简单定义 NULL
为0.所以程序员在技术上可以写 = NULL
,但更清楚使用数字常量0并保留 NULL
用于指针值。)
(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.)
这篇关于将函数的声明设置为0意味着什么?如何为函数分配一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!