Sign in to follow this  

[C++] Overriding interface methods

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using a technique I picked up off CodeProject where virtual methods are not necessary to write an interface. However, there are a select few routines I leave virtual and invite the user to override. A factory method isn't enough to make derived methods available, at least, not on first glance. Since I cannot derive in the manner I'm accustomed to, how can I override methods in an interface properly as a client?

Share this post


Link to post
Share on other sites
This is an interface:
struct IFoo {
virtual ~IFoo() {};
virtual void foo() = 0;
};

struct MyFoo : public IFoo {
virtual void foo() {
//
}
};

Even if there is some hack, I really wouldn't try to break C++ even more by messing around with it.

Implementor of the interface must implement all interface functions. This is consistent in other languages as well.


There is also a concept of abstract base class:
struct AbstractFoo {
virtual ~IFoo() {};
virtual void foo() {} // empty body
};

When extending this interface it is not necessary to override any functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by zyrolasting

What was the danger with the approach shown here? (See "Improved with no Virtual Methods") I saw that approach in FMOD, even.


It took me a while trying to figure out what it all has to do with polymorphism.

The technique is about reducing dependencies in header files by pushing as much as possible into implementation, but without overhead associated with PIMPL. It reduces compilation times.

It is not really an interface, there will always be one and exactly one single concrete class, the interface exists only for the sake of build system.

Share this post


Link to post
Share on other sites
Well, at the end of the day I'm going to need to hide all of the uglier details of my object definition, but there are choice high-level methods I need to keep virtual. I wouldn't want to ask the user to override all methods of the class due to it's size, and I cannot compartmentalize as the the methods are event driven and are in need of the object. I take it there's no easy way out, eh?

Share this post


Link to post
Share on other sites
One of my great revelations from a few years back is that pImpl is actually basically designed specifically to support polymorphism. The pImpl sets up a pointer in order to indirect and hide implementation, and calling virtual functionality requires having a pointer or reference rather than a value (since then the type is known at compile-time). As it happens, you can reuse the same layer of indirection for both goals (compile-time "data hiding" and run-time polymorphism).

Share this post


Link to post
Share on other sites

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this