Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ header file and function declaration ending in "= 0"

I have the following code inside the .h file and I'm not sure what does the assignment statement do and how is it called properly?

virtual void yield() = 0; 

I thought that the function returns a value of 0 by default but since this function returns void I am a little bit confused. Can anyone comment on this and maybe say how can I refer to this assignment, I mean how is it called in C++ jargon?

Thanks.

like image 502
Adam Avatar asked Mar 26 '10 12:03

Adam


People also ask

What does it mean when function 0 C++?

It's just a syntax, nothing more than that for saying that “the function is pure virtual”. A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in declaration.

Which function is declared in header file?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

What is a function header in C?

Function definition The header includes the name of the function and tells us (and the compiler) what type of data it expects to receive (the parameters) and the type of data it will return (return value type) to the calling function or program.

What do you put in header files in C?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .


2 Answers

This is a pure virtual function. This means, that subclasses have to implement this function, otherwise they are abstract, meaning you cannot create objects of that class.

class ISomeInterface { public:     virtual std::string ToString( ) = 0; }  class SomeInterfaceImpl : public ISomeInterface { public:     virtual std::string ToString( ) {         return "SomeInterfaceImpl";     } } 

The idea is, that a class can expose a certain method, but subclasses have to implement it. In this example, ISomeInterface exposes a ToString method, but there is no sensible default implementation for that, so it makes the method pure virtual. Subclasses like SomeInterfaceImpl can then provide a fitting implementation.

like image 196
Björn Pollex Avatar answered Sep 21 '22 16:09

Björn Pollex


The = 0 syntax declares a pure virtual function, it has nothing to do with the return value.

If a class contains at least one pure virtual function, that makes the class "abstract", which means it cannot be instantiated.

In practice, such classes need to be concretized by subclassing and implementing the virtual function(s).

like image 22
unwind Avatar answered Sep 20 '22 16:09

unwind