3

In C++, when a method is declared, I've noticed that sometime the method may have an assignement appended to it.

Could anyone tell me what this is?

For example:

virtual void MyMethod () = 0;

What doe the '= 0' mean. :)

Thanks everyone !!!

Dykam
  • 10,190
  • 4
  • 27
  • 32
Gary Paluk
  • 1,038
  • 1
  • 14
  • 28
  • 7
    I think you mean C++... – Michel de Ruiter Aug 23 '10 at 19:56
  • 3
    C# is just C++ with the streams crossed. (Don't cross the streams) – Ben Voigt Aug 23 '10 at 20:16
  • 2
    possible duplicate of [Why pure virtual function is initialized by 0?](http://stackoverflow.com/questions/2156634/why-pure-virtual-function-is-initialized-by-0) – Hans Passant Aug 23 '10 at 20:30
  • Thanks everyone, I'm understanding it as working like an abstract method that must be overriden (implemented) in a subclass. – Gary Paluk Aug 23 '10 at 21:20
  • C++0x will introduce some new legal values of `x` other than `0`: `delete` to specify that a function doesn't exist (to remove a default constructor or assignment operator, or to prevent an implicit conversion) and `default` to explicitly specify the default implementation of a function. – Steve Jessop Aug 23 '10 at 21:57

3 Answers3

6

It means it's a pure virtual function, i.e. no actual definition of it is available in this class and it must be overridden in a subclass. It's not actually an assignment as such, zero is the only value you can "assign".

And this is C++ syntax; in C# the same would be accomplished with the abstract keyword.

Jakob Borg
  • 23,685
  • 6
  • 47
  • 47
  • This is assuming the OP meant C++, which he probably did. In C# the equivalent to this would be marking the method `abstract` and not giving it a body. – Matt Greer Aug 23 '10 at 19:59
  • 2
    Not even any zero value will hold. *Only `0` is valid here*, it's put straight as a grammar symbol. `0L` or something else won't work. I.e while it may work on some compilers to use `NULL` instead (because it expands to `0` for them), on others it may not work :) – Johannes Schaub - litb Aug 23 '10 at 20:15
  • 3
    The bit about no definition isn't necessarily true. Rather, it just means a derived class needs to implement the function. If it is not, it cannot be instantiated. – GManNickG Aug 23 '10 at 20:33
  • GMan is correct. Even if you make your destructor pure virtual you still need to provide a definition for it. Pure virtual means that the function must be defined in any subclass before that subclass can be instantiated. – Bill Aug 23 '10 at 20:42
  • @GMan, @Bill, isn't that exactly what I said? – Jakob Borg Aug 24 '10 at 06:39
  • 2
    @calmh: No, you still claim that "no actual definition is available". Bill even came up with an example in which an actual definition _must_ be available. – MSalters Aug 24 '10 at 06:47
  • Ah the "destructor" part, I missed that. True. – Jakob Borg Aug 24 '10 at 14:34
4

In C++ this means that the method is a pure virtual method.

This means that an instance of this particular class-type cannot be instantiated. You can only create instances of classes derived from this, which override all pure virtual methods in the base class.

A base class with pure virtual methods defines an interface that derived classes have to implement, and is not meant to be used on its own.


Contrary to what calmh claims, as far as I know pure virtual functions can be implemented and they can be called explicitly.

#include <cstdio>

class A
{
public:
    virtual void foo() const = 0; //pure virtual
};

void A::foo() const { puts("A::foo"); }

class B: public A
{
public:
    virtual void foo() const { puts("B::foo"); }
};

int main()
{
    //A a;  //this would be an error - main point of having pure virtual functions
    B b;
    b.foo();
    b.A::foo();
}

Usually one wouldn't do this, though, except perhaps if the virtual destructor is pure in a base class (in this case it has to be defined).

UncleBens
  • 40,819
  • 6
  • 57
  • 90
  • Also, (now, i'm entering a bit pedantry mode) contrary to what he writes, any subclass can leave off overriding it. It will then also be abstract, but if no-one creates an object of it (except as a base class subobject in turn), that's fine. – Johannes Schaub - litb Aug 23 '10 at 20:18
2

In C#, that is a syntax error.

If you meant C++, see calmh's answer.

James Curran
  • 101,701
  • 37
  • 181
  • 258