Sign in to follow this  
Wavesonics

C++ Abstract classes as Interfaces

Recommended Posts

Wavesonics    330
So I'm trying to use a pure virtual class as an Interface. Thus I could have a function accept the interface like:
Quote:
class SomeInterface { virtual void interfaceFunction() = 0; } void f( SomeInterface obj );
But I get a compilation error obviously saying "SomeInterface does not implement pure virtual function 'interfaceFunction'". So I did:
Quote:
class SomeInterface { virtual void interfaceFunction() {} }
Which works, but of course, now does not require inheriting classes to over ride the interface function. So how should this be done?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
f( SomeInterface obj ); defines a function which takes an instance of SomeInterface, which is impossible (such an instance cannot exist, because SomeInterface is abstract).

Have f take a reference or pointer to an object instead.

Share this post


Link to post
Share on other sites

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