views:

161

answers:

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 !!!

+6  A: 

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.

calmh
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
@Matt Yes. My edit at the same time as your comment. :)
calmh
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
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.
GMan
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
@GMan, @Bill, isn't that exactly what I said?
calmh
@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
Ah the "destructor" part, I missed that. True.
calmh
+2  A: 

In C#, that is a syntax error.

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

James Curran
+4  A: 

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