windows 将函数的声明设置为 0 是什么意思?如何给一个函数赋值一个整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6634358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean to set the declaration of a function equal to 0? How can you assign an integer to a function?
提问by ApprenticeHacker
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:
当我在窗口类中找到以下函数定义时,我正在浏览一个(不喜欢命名)GUI 工具包的源代码,该工具包包装了 Windows API:
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?
这里发生了什么?如何将函数分配给整数?或者它是否将它分配给NULL
?如果要在 wndproc 中使用函数指针,是否需要这样做?
回答by Cody Gray
That line of code defines a pure virtual functionin C++. It has nothing to do with the otherwise tricky Win32 API or GUI code in general.
这行代码在 C++ 中定义了一个纯虚函数。一般而言,它与其他棘手的 Win32 API 或 GUI 代码无关。
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" classand 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.
C++ 使用特殊语法= 0;
来指示纯虚函数,而不是向语言添加新关键字(就像 C# 之类的语言所做的那样)。您可以将其视为将函数指针设置为 0。
Also see the answers to this related question: What are the uses of pure virtual functions in C++?
另请参阅此相关问题的答案:C++ 中纯虚函数的用途是什么?
(By the way, the Windows header files <windows.h
> simply define NULL
as 0. So the programmer technically couldhave written = NULL
, but it's much clearer to use the numeric constant 0 and reserve NULL
for pointer values.)
(顺便说一下,Windows 头文件 < windows.h
> 简单地定义NULL
为 0。所以程序员在技术上可以编写= NULL
,但使用数字常量 0 并保留NULL
指针值更清晰。)
回答by Alok Save
It is a pure virtual function.
它是一个纯虚函数。
The =0
is just the syntax used to indicate that it is a pure virtual function.
这=0
只是用于表示它是纯虚函数的语法。
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.
有时添加纯虚函数只是为了使基类抽象(因此无法创建它的实例)。通常,在这种情况下,不是添加虚拟函数来使类抽象,而是使类的析构函数成为纯虚拟的。
回答by Uwe Keim
This is a "pure virtual" function that you need to override in a derived class.
这是一个“纯虚拟”函数,您需要在派生类中覆盖它。