tags:

views:

166

answers:

8

Possible Duplicate:
Why pure virtual function is initialized by 0?

In C++, what does it mean to set a function declaration to zero? I'm guessing it has to do with the function being virtual, and not actually defined in this class. Found in a header file of code that I'm reading:

virtual void SetValue(double val)=0;

What's going on here?

A: 

It means that, the class is abstract, and can't be create its object. It could be used as a base class to another.
Pure virtual class is also used in c++ as an interface known from language like java, but of course it is different.

Rin
+7  A: 

It's a pure virtual function. It makes it so you MUST derive a class (and implement said function) in order to use it.

Cogwheel - Matthew Orlando
In addition to deriving, you must implement them as well in the derived classes.
bits
Which results in an abstract class.
Bruno Brant
A: 

if you set a virtual function to set, Its called Pure Virtual Functions. And then your class becomes an abstract class. You can't create instances of that class or any subclasses unless your pure virtual functions are implemented.

bits
+1  A: 

It means that it is a pure virtual method - which means subclasses must implement the method on their own. There can still be an implementation for that method, but classes with pure virtual methods cannot be instantiated, making this similar to the abstract keyword seen in various other languages.

Michael Madsen
Despite the name a pure virtual method can have an implementation... a declaration of pure virtual means that the class is abstract and that a derived class must implement the function to be non-abstract. It doesn't mean that the method has no implementation in the base class (actually it CAN have an implementation in the base class).
6502
Ah yes, of course. My bad. I'll edit my post.
Michael Madsen
Nit pick: C++ has functions, not methods. The correct term is *pure virtual function*.
Brian Neal
+1  A: 

This is called a pure virtual member function in C++ lingo. It means, as you guessed, that the function is not defined within the class, but rather has to be implemented in deriving classes. You cannot instantiate classes with pure virtual member functions, pure virtual functions basically behave like abstract methods in Java or C#.

haffax
It doesn't mean that it's not defined, just that it must be overridden. It can have a definition.
Mike Seymour
Yes, you are right.
haffax
A: 

The method SetValue is pure virtual. Its class does not provide the implementation of that method, and can not be instantiated (therefore we term it abstract). Concrete derived classes on the other hand have to provide implementations for such methods. See more here.

ssegvic
A: 

As everyone has mentioned, this means that the function is a pure virtual function. Think of of this as setting the function pointer to null. Classes with pure virtual functions are handled as abstract classes. This means that derived classes must implement this virtual function.

Occasionally, you may encounter what is called a "pure call" error. This means that a pure virtual function was actually called and it will most likely cause the program to crash. The most common cause of a pure call is that the object that the function was called on was already deleted.

Mike
+1  A: 

"SetValue" is a pure virtual member function that the base class forces derived classes to provide. Pure virtual member functions will have no implementation.

A pure virtual member function specifies that a member function will exist on every object of a concrete derived class even though the member function is not (normally) defined in the base class.

This is because the syntax for specifying a pure virtual member function forces derived classes to implement the member function if the derived classes intend to be instantiated (that is, if they intend to be concrete).

In your case, all objects of classes derived from the base class will have the member function SetValue(). However, because the base class is an abstract concept, it does not contain enough information to implement SetValue().

Imagine that the "= 0" is like saying "the code for this function is at the NULL pointer."

Pure virtual member functions allow users to write code against an interface for which there are several functionally different variants. This means that semantically different objects can be passed to a function if these objects are all under the umbrella of the same abstract base class.

Prabhu Jayaraman