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

后端 未结 3 415
故里飘歌
故里飘歌 2020-12-24 04:48

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:

3条回答
  •  庸人自扰
    2020-12-24 05:32

    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.

提交回复
热议问题