4

I need to understand these statements:

virtual string FOOy() = 0;
virtual string FOOx( bool FOOBAR ) = 0;

I am not sure if the function being virtual has anything to do with it...

nulltorpedo
  • 1,195
  • 2
  • 12
  • 21

6 Answers6

7

Although your testcase is woefully incomplete, from the presence of the keyword virtual it looks like this is inside a class definition.

In such a context, = 0 is not an assignment at all, but a piece of confusing syntax that marks the virtual member function as being "pure". A pure virtual member function may have an implementation (defined elsewhere), but one is optional and the function's very existence prohibits the class from being instantiated.

That is, a class with pure virtual member functions may be called "abstract".

Your peer-reviewed C++ book covers the topic in much greater detail.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • "confusing syntax" - a note that might help: a typical compiler creates a virtual dispatch table (VDT) - an array of pointers to member functions - for each class with virtual functions. For the common case of a pure virtual function with no implementation, there's no programmer-specified function to point to. "= 0" is suggestive of a 0/NULL pointer-to-member-function entry in the VDT, which is intuitive but may be misleading as 1) VDT mechanisms aren't specified in the Standard / compiler could do anything, 2) pure virtual functions can have implementations and corresponding non-0 ptrs. – Tony Delroy Feb 10 '12 at 02:49
  • @TonyDelroy: Precisely right. In fact, before I discovered all those years ago that pure virtual member functions may have implementations, it made complete sense to me that you could somehow (at least stylistically) "assign a null function pointer" to the declaration; now of course I know that it's just downright misleading. – Lightness Races in Orbit Feb 10 '12 at 10:05
  • Thanks for the peer-reviewed link, its a great compilation. – linuxeasy Feb 10 '12 at 10:55
  • dead link, darn it –  Dec 31 '16 at 20:27
2

It means that the method is pure, or abstract. It means that the method is meant to be declared by extending classes (thanks for clarifying this--see comments below).

Zenexer
  • 18,788
  • 9
  • 71
  • 77
2

The = 0 syntax is how you declare a pure virtual function in C++. A pure virtual has no implementation in the class declaring it -- any subclass must implement the function in order to be instantiable.

http://www2.research.att.com/~bs/glossary.html#Gpure-virtual-function

aalpern
  • 652
  • 4
  • 8
  • 1
    [Pure virtual member functions may absolutely be defined.](http://ideone.com/79RSR) – Lightness Races in Orbit Feb 10 '12 at 01:24
  • Whoops, I momentarily forgot that you actually can do that. I hardly ever see it done, however. – aalpern Feb 10 '12 at 01:25
  • Recently, mostly my employer's code base. I think it's on the order of 100K lines, but not much of it involves pure virtuals. I tend to declare pure virtuals in interface classes (composed of pure virtuals only - say, IWidget), and put the default implementations in abstract bases (i.e. WidgetBase : public IWidget). I think that leaves the most flexibility. YMMV, of course. – aalpern Feb 10 '12 at 03:39
  • If `WidgetBase` is an abstract base then there's not much difference there ;) – Lightness Races in Orbit Feb 10 '12 at 10:06
2

That makes the function a pure virtual function. This means that the class that declares the function is abstract, and subclasses must provide an implementation for this function.

Mike Daniels
  • 8,582
  • 2
  • 31
  • 44
2

By adding the = 0 you are declaring the virtual function to be pure virtual function. This means that derived classes must implement the method before they can be instantiated. Normally the base class does not have implementation.

This is also called an abstract function in other languages, such as Java and C#.

David V
  • 11,531
  • 5
  • 42
  • 66
0

It simply means, that the implementor (Original writer) of the class in which FOOx and FOOy intended it to be used as an interfaces to its Derived Classes.

And these being virtual means, it will be possible that the derived class' implementation will be used, using the base class' pointer. So its being usable as an interface becomes possible by declaring them as virtual.

And finally, answering your question. Value-assignment, specifically assigning 0 to a function means, explicitly saying, that function doesn't has any definition. (Though you can specify a definition for it, but it will need to be called explicitly by the derived classes)

linuxeasy
  • 6,269
  • 7
  • 33
  • 40