• <i id='36rok'><tr id='36rok'><dt id='36rok'><q id='36rok'><span id='36rok'><b id='36rok'><form id='36rok'><ins id='36rok'></ins><ul id='36rok'></ul><sub id='36rok'></sub></form><legend id='36rok'></legend><bdo id='36rok'><pre id='36rok'><center id='36rok'></center></pre></bdo></b><th id='36rok'></th></span></q></dt></tr></i><div id='36rok'><tfoot id='36rok'></tfoot><dl id='36rok'><fieldset id='36rok'></fieldset></dl></div>

    1. <tfoot id='36rok'></tfoot>

      <small id='36rok'></small><noframes id='36rok'>

        <bdo id='36rok'></bdo><ul id='36rok'></ul>

    2. <legend id='36rok'><style id='36rok'><dir id='36rok'><q id='36rok'></q></dir></style></legend>

      1. 如何在 C++ 中实现接口?

        时间:2023-09-26
          • <i id='DVRkL'><tr id='DVRkL'><dt id='DVRkL'><q id='DVRkL'><span id='DVRkL'><b id='DVRkL'><form id='DVRkL'><ins id='DVRkL'></ins><ul id='DVRkL'></ul><sub id='DVRkL'></sub></form><legend id='DVRkL'></legend><bdo id='DVRkL'><pre id='DVRkL'><center id='DVRkL'></center></pre></bdo></b><th id='DVRkL'></th></span></q></dt></tr></i><div id='DVRkL'><tfoot id='DVRkL'></tfoot><dl id='DVRkL'><fieldset id='DVRkL'></fieldset></dl></div>
                <bdo id='DVRkL'></bdo><ul id='DVRkL'></ul>
                  <tbody id='DVRkL'></tbody>

                <tfoot id='DVRkL'></tfoot>

                <small id='DVRkL'></small><noframes id='DVRkL'>

                  <legend id='DVRkL'><style id='DVRkL'><dir id='DVRkL'><q id='DVRkL'></q></dir></style></legend>

                  本文介绍了如何在 C++ 中实现接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  在 C++ 中模拟接口的首选方法

                  我很想知道 C++ 中是否有接口,因为在 Java 中,设计模式的实现主要是通过接口将类解耦.那么在 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++ 没有内置的接口概念.您可以使用仅包含 纯虚函数.因为它允许多重继承,你可以继承这个类来创建另一个类,然后在其中包含这个接口(我的意思是,对象接口 :)).

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

                  这篇关于如何在 C++ 中实现接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以通过引用以基类为参数的函数来传递派生类 下一篇:在 C++ 中为抽象类模板创建接口

                  相关文章

                  <tfoot id='di0Op'></tfoot>

                    1. <small id='di0Op'></small><noframes id='di0Op'>

                        <bdo id='di0Op'></bdo><ul id='di0Op'></ul>
                      <legend id='di0Op'><style id='di0Op'><dir id='di0Op'><q id='di0Op'></q></dir></style></legend>
                    2. <i id='di0Op'><tr id='di0Op'><dt id='di0Op'><q id='di0Op'><span id='di0Op'><b id='di0Op'><form id='di0Op'><ins id='di0Op'></ins><ul id='di0Op'></ul><sub id='di0Op'></sub></form><legend id='di0Op'></legend><bdo id='di0Op'><pre id='di0Op'><center id='di0Op'></center></pre></bdo></b><th id='di0Op'></th></span></q></dt></tr></i><div id='di0Op'><tfoot id='di0Op'></tfoot><dl id='di0Op'><fieldset id='di0Op'></fieldset></dl></div>