如何在c++中实现接口?(复制)

时间:2022-09-02 08:29:03

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

可能的重复:在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?

我很想知道c++中是否有接口,因为在Java中,设计模式的实现主要是通过接口解耦类。那么在c++中有类似的创建接口的方法吗?

3 个解决方案

#1


75  

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.

c++没有接口的内置概念。您可以使用只包含纯虚函数的抽象类来实现它。由于它允许多重继承,所以您可以继承这个类来创建另一个类,该类将在其中包含这个接口(我的意思是对象接口:)。

A sample 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;
}

#2


12  

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.

在c++中没有接口的概念,您可以使用抽象类来模拟行为。抽象类是具有至少一个纯虚函数的类,不能创建抽象类的任何实例,但可以创建指向它的指针和引用。此外,继承抽象类的每个类必须实现纯虚函数,以便创建其实例。

#3


10  

Interface are nothing but a pure abstract class in C++. Ideally this interface class should contain only pure virtual public methods and static const data. e.g.

接口在c++中只是一个纯粹的抽象类。理想情况下,这个接口类应该只包含纯虚拟公共方法和静态const数据。如。

class InterfaceA
{
public:
  static const int X = 10;

  virtual void Foo() = 0
  virtual int Get() const = 0;
  virtual inline ~InterfaceA() = 0;
};
InterfaceA::~InterfaceA () {}

#1


75  

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.

c++没有接口的内置概念。您可以使用只包含纯虚函数的抽象类来实现它。由于它允许多重继承,所以您可以继承这个类来创建另一个类,该类将在其中包含这个接口(我的意思是对象接口:)。

A sample 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;
}

#2


12  

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.

在c++中没有接口的概念,您可以使用抽象类来模拟行为。抽象类是具有至少一个纯虚函数的类,不能创建抽象类的任何实例,但可以创建指向它的指针和引用。此外,继承抽象类的每个类必须实现纯虚函数,以便创建其实例。

#3


10  

Interface are nothing but a pure abstract class in C++. Ideally this interface class should contain only pure virtual public methods and static const data. e.g.

接口在c++中只是一个纯粹的抽象类。理想情况下,这个接口类应该只包含纯虚拟公共方法和静态const数据。如。

class InterfaceA
{
public:
  static const int X = 10;

  virtual void Foo() = 0
  virtual int Get() const = 0;
  virtual inline ~InterfaceA() = 0;
};
InterfaceA::~InterfaceA () {}