how to implement Interfaces in C++? [duplicate]
Asked Answered
P

3

116

Possible Duplicate:
Preferred way to simulate interfaces in C++

I was curious to find out if there are interfaces in C++ because in Java, there is the implementation of the design patterns mostly with decoupling the classes via interfaces. Is there a similar way of creating interfaces in C++ then?

Puerperium answered 18/3, 2012 at 8:2 Comment(1)
Do you really need interfaces? C++ has a vast support for templates. Just write your functions receiving templates, and ensure the passed types have the require methods to don't cause compile errors, so you don't impose your users to inherit from anything to use your methods.Julienne
A
169

C++ has no built-in concepts of interfaces. You can implement it using abstract classes which contains only pure virtual functions. Since it allows multiple inheritance, you can inherit this class to create another class which will then contain this interface (I mean, object interface :) ) in it.

An example would be something like this -

class Interface
{
public:
    Interface(){}
    virtual ~Interface(){}
    virtual void method1() = 0;    // "= 0" part makes this method pure virtual, and
                                   // also makes this class abstract.
    virtual void method2() = 0;
};

class Concrete : public Interface
{
private:
    int myMember;

public:
    Concrete(){}
    ~Concrete(){}
    void method1();
    void method2();
};

// Provide implementation for the first method
void Concrete::method1()
{
    // Your implementation
}

// Provide implementation for the second method
void Concrete::method2()
{
    // Your implementation
}

int main(void)
{
    Interface *f = new Concrete();

    f->method1();
    f->method2();

    delete f;

    return 0;
}
Anachronous answered 18/3, 2012 at 8:3 Comment(7)
@CharlesBailey: Sorry, forgot to mention that. Thanks for catching that! Is it ok now?Anachronous
@CharlesBailey: I hope it's OK now.....Anachronous
Now it looks OK to me. It compiles and valgrind doesn't report any errors. What compiler did you use?Relique
@CharlesBailey: Well, I didn't use any compiler, wrote it down from my memory....probably that's why I missed so many things...Anachronous
come on 2018 still no basic interfaces system in C++, f@# sake, I hate having no other choices than C++Decosta
wouldn't it be cleaner to use the override keyword instead of =0 ?Slug
@fdsfdsfdsfds C++ has interfaces. C++ does not have a keyword interface.Hiller
G
23

An "Interface" is equivalent to a pure abstract class in C++. Ideally this interface class should contain only pure virtual public methods and static const data members. For example:

struct MyInterface
{
  static const int X = 10;

  virtual void Foo() = 0;
  virtual int Get() const = 0;
  virtual inline ~MyInterface() = 0;
};
MyInterface::~MyInterface () {}
Gervase answered 18/3, 2012 at 8:4 Comment(5)
What about a virtual destructor?Relique
I'd add a virtual dtor to avoid possible memory leaks in the future.Switzer
@CharlesBailey, missed that. Edited the post. Added as pure virtual just to maintain the pure abstractness of the class and inline so that it can reside in the header file without linker errors.Gervase
It would be simpler just to define the destructor in the class definition. You could just do virtual ~interfaceA() {}. Your class is already abstract so you don't gain anything by making the destructor a pure virtual function.Relique
@barankin: Memory leaks have nothing to do with virtual destructors. It's simply about writing correct C++.Marplot
H
17

There is no concept of interface in C++,
You can simulate the behavior using an Abstract class.
Abstract class is a class which has atleast one pure virtual function, One cannot create any instances of an abstract class but You could create pointers and references to it. Also each class inheriting from the abstract class must implement the pure virtual functions in order that it's instances can be created.

Harriettharrietta answered 18/3, 2012 at 8:4 Comment(4)
And how is this not the concept of interfaces? Because abstract base classes allow in theory even more? Or because the keyword is not “interface”?Buddhology
@IceFire: Because C++ does not have a keyword interfaceHarriettharrietta
Ah. So if a language had something “like” interfaces but had a short form like “ifc”, it would also not have the concept of interfaces? And JS (before EC6) had no classes because you would need to write “function”? Does Java also have no generics because it has no “generic” keyword?Buddhology
Top marks @Buddhology for pointing out a broad range of incongruences between languages, concepts and definitions. If there is no concept of an interface in C++, how can the behaviour be, in Alok Save's own words, "simulated with an Abstract class"?Smuts

© 2022 - 2024 — McMap. All rights reserved.