-1

I am not sure of my interpretation of these instructions:

virtual int getRadioChannel(RadioRef r) const = 0;
virtual int getNumChannels() = 0;

For me, the first one means that a called of the function getRadioChannel returns always 0 and the second one does nothing when the function getNumChannels is called. Am I right ?

Thanks in advance.

Konstantin Vladimirov
  • 6,791
  • 1
  • 27
  • 36
guetguet
  • 13
  • 2
  • http://en.wikipedia.org/wiki/Virtual_function#Abstract_classes_and_pure_virtual_functions – chris Jan 31 '13 at 09:23
  • 1
    "virtual function header" is already the wrong term. What book are you using to learn C++ and the terms used in that context? – PlasmaHH Jan 31 '13 at 09:35

4 Answers4

5

Am I right ?

No, not at all.

These are declarations of functions that are intended to form, in part, an interface.

Read about pure virtual functions in your C++ book.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

These are both declaring pure virtual functions; functions without definition in the class they are declared in. Since there are no definitions of the functions, the class cannot be instantiated; only subclasses of the class, which do have these functions defined, can.

In the first case, it's also const function -- essentially, the you're promising not to modify the object in the body of the function. (Specifically: the this pointer in the function body will be const, and the function is callable on const objects.) The second case is just a "normal" pure virtual function declaration.

You can read more about pure virtual functions here, and const functions here.

sheu
  • 5,653
  • 17
  • 32
1

You're not right, the =0 signifies a pure virtual function.

That makes the class abstract and forces all non-abstract derived classes to implement the methods marked as pure.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0
virtual int getRadioChannel(RadioRef r) const { return 0; }

This is what a function that always returns 0 would look like. As others have pointed out, equating the function to 0, makes it a pure virtual function.

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class, if that class is not abstract. Classes containing pure virtual methods are termed "abstract"; they cannot be instantiated directly. A subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class.

Tadeusz Kopec for Ukraine
  • 12,283
  • 6
  • 56
  • 83
Karthik T
  • 31,456
  • 5
  • 68
  • 87