views:

330

answers:

6

I recently came to know that in C++ pure virtual functions can optionally have a body.

What are the real-world use cases for such functions?

+10  A: 

Pure virtual functions with or without a body simply mean that the derived types must provide their own implementation.

Pure virtual function bodies in the base class are useful if your derived classes wants to call your base class implementation.

Brian R. Bondy
+3  A: 

One use case is calling the pure virtual function from the constructor or the destructor of the class.

shoosh
+1: This is in fact the main case. Some might say the only case.
John Dibling
But there's no such thing as a pure virtual constructor, which your post seems to imply.
John Dibling
@John: I read it different. However, I think calling virtual functions, pure or not, from ctors or detors is a pretty questionable practice anyway.
sbi
Meh, it has very well defined behavior in the language, it never calls the override.
Hans Passant
@sbi: after reading your comment I re-read this post and found my reading comprehension circuits completely failed. I thought that @shoosh was saying what you actually said in your accepted reply. I would remove my u/v if I could.
John Dibling
@nobugz: Which is exactly why it is questionable. If you don't need the virtual function mechanism to kick in, why is the function virtual in the first place? I don't dispute that there are cases where doing this might be Ok (I wrote _questionable_, after all, not _wrong_), but I suppose they are pretty rare.
sbi
@sbi - one reason may be that you don't want to write the same function as both non-virtual and virtual.
shoosh
+3  A: 

The only difference of virtual function with body and pure virtual function with body is that existence of second prevent instantiation. You can't mark class abstract in c++.

Andrey
+6  A: 

The classic is a pure virtual destructor:

class abstract {
  public: 
    virtual ~abstract() = 0;
};

abstract::~abstract() {}

You make it pure because there's nothing else to make so, and you want the class to be abstract, but you have to provide an implementation nevertheless, because the derived classes' destructors call yours explicitly. Yeah, I know, a pretty silly textbook example, but as such it's a classic. It must have been in the first edition of The C++ Programming Language.

Anyway, I can't remember ever really needing the ability to implement a pure virtual function. To me it seems the only reason this feature is there is because it would have had to be explicitly disallowed and Stroustrup didn't see a reason for that.

If you ever feel you need this feature, you're probably on the wrong track with your design.

sbi
+1 Best answer in the thread.
John Dibling
One issue that a downvoter might have (for the record, I didn't downvote) is that there's really no strong reason to have a pure virtual destructor. If there's another pure virtual function, there's no reason for the dtor to be pure virtual (though it almost certainly should be virtual). If the dtor is the only pure virtual function, then there may really be no need for the class to be abstract.
Michael Burr
@Michael: I know this critique. (In fact, I wrote pretty much that in my answer.) The problem is, I can't think of any good reason to implement a pure virtual function myself. As I wrote, if you feel you need to do that, it's pretty likely you're on the wrong track.
sbi
Anyway, I think if someone thought this answer is bad, I would have been interested to know why. But it seems commenting on why you down-vote is seriously getting out of fashion.
sbi
+6  A: 

One reason that an abstract base class (with a pure virtual function) might provide an implementation for a pure virtual function it declares is to let derived classes have an easy 'default' they can choose to use. There isn't a whole lot of advantage to this over a normal virtual function that can be optionally overridden - in fact, the only real difference is that you're forcing the derived class to be explicit about using the 'default' base class implementation:

class foo {
public:
    virtual int interface();
};

int foo::interface() 
{
    printf( "default foo::interface() called\n");
    return 0;
};


class pure_foo {
public:
    virtual int interface() = 0;
};

int pure_foo::interface()
{
    printf( "default pure_foo::interface() called\n");
    return 42;
}

//------------------------------------

class foobar : public foo {
    // no need to override to get default behavior
};

class foobar2 : public pure_foo {
public:
    // need to be explicit about the override, even to get default behavior
    virtual int interface();
};

int foobar2::interface()
{
    // foobar is lazy; it'll just use pure_foo's default
    return pure_foo::interface();
}

I'm not sure there's a whole lot of benefit - maybe in cases where a design started out with an abstract class, then over time found that a lot of the derived concrete classes were implementing the same behavior, so they decided to move that behavior into a base class implementation for the pure virtual function.

I suppose it might also be reasonable to put common behavior into the pure virtual function's base class implementation that the derived classes might be expected to modify/enhance/augment.

Michael Burr
+3  A: 

The almighty Herb Sutter, former chair of the C++ standard committee, did give 3 scenarios where you might consider providing implementations for pure virtual methods.

Gotta say that personally – I find none of them convincing, and generally consider this to be one of C++'s semantic warts. It seems C++ goes out of its way to build and tear apart abstract-parent vtables, than exposes them briefly only during child construction/destruction, and then the community experts unanimously recommend never to use them.

Ofek Shilon
That Sutter article is excellent. Cleared up some obscure ideas for me.
DarenW