58

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.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
Adam
  • 581
  • 1
  • 4
  • 3
  • 2
    read about virtual functions. – N 1.1 Mar 26 '10 at 12:34
  • 1
    And for info on why this strange syntax is used, see http://stackoverflow.com/questions/2156634/why-pure-virtual-function-is-initialized-by-0 –  Mar 26 '10 at 12:55

4 Answers4

51

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.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • The OP asks *"how is it called in C++ jargon?"* - this is answered in the first sentence of my answer. If you feel that this needs further explanation, feel free to edit my answer, add an answer of your own or explain in a comment. – Björn Pollex Apr 24 '14 at 07:13
  • Ok. Fair enough. I was confused by the "= 0", but, think I ended up figuring this out. The "= 0" is what tells the compiler there will not be a body for the function, this, making it a "pure virtual". Correct? – Doo Dah Apr 24 '14 at 20:30
  • Not quite. Pure virtual functions [can have a body](http://www.gotw.ca/gotw/031.htm). See [this question](http://stackoverflow.com/questions/2156634) for an explanation of the syntax. – Björn Pollex Apr 25 '14 at 07:02
20

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

unwind
  • 391,730
  • 64
  • 469
  • 606
4

if is a pure virtual method (aka abstract) look here or google http://www.artima.com/cppsource/pure_virtual.html

= 0 doesn't mean default return value, it is notification that function is pure virtual

Andrey
  • 59,039
  • 12
  • 119
  • 163
1

The syntax is obscure, but the "=0" signifies the method is a pure virtual function. It makes the class abstract (you can't instantiate it) and it's implementation is left to derived classes.

This is used when all you want to define is an interface. Use the virtual keyword when you want to define an interface and also provide a default implementation.

Dave M
  • 1,302
  • 1
  • 16
  • 28