Reputation: 708
In Java, you can define an interface as a class with no actual code implementation, but only to define the methods that a class must implement. Those types can be passed as parameters to methods and returned from methods. In C++, a pure virtual class can't be used as a parameter or return type, from what I can tell. Any way to mimic Java's interface classes?
I have a string class in C++, and several subclasses for different encodings (like UTFxxx, ISOxxx, etc) that derive from the base string class. However, since there are so many different encodings, the base class has no meaningful implementation. But it would serve well as an interface if I could handle it as its own object and calls to that object would call on the correct subclass it was inherited to.
Upvotes: 2
Views: 2278
Reputation: 3322
A pure-virtual class can be used as a return type iff you only use reference or pointer.
In fact, in Java, Interface foo = Implementer();
is equivalent to Interface* foo = Implementer();
and implements Foo
is public virtual Foo
.
Here is a Java example:
interface Foo {
public void doFoo();
}
class Bar implements Foo {
public void doFoo() { }
}
Its C++ counterpart:
struct Foo {
virtual void doFoo() = 0;
virtual ~Foo() { }
};
class Bar : public virtual Foo {
public:
virtual void doFoo() { }
virtual ~Bar() { }
};
Rightly, C++ will whine about incomplete type if you use Foo foo;
. Using Foo& foo
, Foo* foo
, or smart_pointer<Foo> foo
is what is required.
In this example smart_pointer
can be scoped_ptr
, unique_ptr
, or shared_ptr
, depending on if you want
delete()s
on destructionNote that boost smart pointers, should you use them, use get()
/reset()
semantics for getting and setting the pointer value.
Upvotes: 3
Reputation: 2592
Here is the definition of interface in Google C++ Style:
A class is a pure interface if it meets the following requirements:
An interface class can never be directly instantiated because of the pure virtual method(s) it declares. To make sure all implementations of the interface can be destroyed correctly, the interface must also declare a virtual destructor (in an exception to the first rule, this should not be pure). See Stroustrup, The C++ Programming Language, 3rd edition, section 12.4 for details.
Take a look at Google C++ Style Guide, section "Multiple Inheritance" and "Interface". http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
Upvotes: 1
Reputation: 6540
You can certainly use a class like an interface, but it's not a construct of the language, it's just a class with only pure virtual methods. Remember, interfaces were just how Java did multiple inheritance without all the tricky problems (two copies of a grandparent class, multiple variables with the same name, etc)
Upvotes: 1
Reputation: 9028
You cannot use a pure virtual class by value, but you can pass it around by pointer or by reference. This includes the ability to pass it around wrapped within a smart pointer type such as unique_ptr
or shared_ptr
.
You should also make sure that you mark the destructor on the interface with the virtual
keyword to prevent potential memory leaks.
Upvotes: 2