简体   繁体   English

C ++抽象类

[英]C++ abstract classes

I am new to C++, I coded in Java for an year. 我是C ++的新手,我用Java编写了一年。 The equivalent in C++ for interfaces and abstract classes alike is only abstract classes. 接口和抽象类的C ++中的等价物只是抽象类。 Is that supposed to be a handicap while doing factory design? 在做工厂设计时,这应该是一个障碍吗? There are many times I want to leave out the defining the methods to the inheriting classes and I want to enforce that. 有很多次我想省略继承类的方法定义,我想强制执行。

If you define like this you can have what you want: 如果你这样定义,你可以拥有你想要的东西:

class A {
  public:
    virtual void pure_virtual(int param1) = 0;
    virtual ~A() { };
}; 

EDIT : Thanks Nikolai and Mike! 编辑 :谢谢尼古拉和迈克!

Java interfaces are best translated as C++ abstract classes. Java接口最好翻译为C ++抽象类。 In Java (as far as I know) a interface is merely a subset of an abstract class anyway, except it allows "multiple inheritance" which C++ has in all cases anyway. 在Java中(据我所知),接口只是抽象类的一个子集,除了它允许C ++在所有情况下都具有的“多重继承”。

class thing_interface {
public:
    virtual ~thing_interface() {}
    virtual void foo() =0; //pure virtual function
};
class other_interface {
public:
    virtual ~other_interface () {}
    virtual void bar() =0; //pure virtual function
};

class thing_impl : public thing_interface, public other_interface { 
protected:
    int member;
public:
    thing_impl() : member(0) {}
    virtual ~thing_impl() {};
    virtual void foo() { std::cout << "FOO!\n";}
    virtual void bar() { std::cout << "BAR!\n";}
};

The =0 syntax means it is not defined in the interface, but must be defined in classes that fulfill the interface. =0语法表示它未在接口中定义,但必须在满足接口的类中定义。 Also note that if you have any virtual function, you almost always want a virtual destructor as well. 另请注意,如果您有任何虚函数,那么您几乎总是需要虚拟析构函数。 C++ will tell you an error if you try to make a thing_interface by itself, since there's no implementation functions. 如果你试图自己创建一个thing_interface ,C ++会告诉你一个错误,因为没有实现函数。

It's not really a handicap, since I can't think of anything Java can do here that C++ can't. 这不是一个障碍,因为我无法想象Java可以做的任何东西,C ++不能。

In terms of interface/abstract classes you wont feel handicap. 在界面/抽象类方面,你不会感到有障碍。 You should read about C++ pure virtual functions and abstract classes. 您应该阅读有关C ++纯虚函数和抽象类的内容。

To have interface in C++, here is the sudo code: 要在C ++中使用接口,这里是sudo代码:

class MyInterface {
  virtual void func1() = 0;
  virtual int func2(int x, int y) = 0;
}


class MyAnotherInterface {
  virtual void func3() = 0;
  virtual int func4(int x, int y) = 0;
}

To have abstract class in C++, here is the sudo code. 要在C ++中使用抽象类,这里是sudo代码。 You can see it only implements one function from the interface above. 你可以看到它只从上面的接口实现了一个功能。 So you cannot create instance of it. 所以你不能创建它的实例。

class MyAbstract : public MyIterface {
  void func1() {
    printf("in func1");
  }
}

Now the actual concrete class: 现在实际的具体类:

class MyConcreteClass : public MyAbstract, public MyAnotherIterface {
  int func2(int x, int y) {
    printf("func2 x=%x,y=%y",x,y);
  }

  void func3() {
    printf("in func3");
  }

  int func4(int x, int y) {
    printf("func4 x=%x,y=%y",x,y);
  }
}

There are some issues when you are using Multiple inheritance as I am using for MyConcreteClass. 当您使用多重继承时,存在一些问题,因为我正在使用MyConcreteClass。 But if you only have one base with member variables and other base classes contain only pure virtual functions then this pattern acts exactly like Java, where the class containing some methods and member variables maps to extends and other classes which contain only pure virtual functions maps to `implements'. 但是如果你只有一个带有成员变量的基类而其他基类只包含纯虚函数,那么这个模式就像Java一样,其中包含一些方法和成员变量的类映射到extends而其他只包含纯虚函数的类映射到'工具。

In our example, Java equivalent code is 在我们的示例中,Java等效代码是

class MyConcreteClass extends MyAbstract implements MyAnotherInterface {
  public int func2(int x, int y) {
    System.out.print(String.format("func2 x=%x,y=%y",x,y));
  }

  public void func3() {
    System.out.print("in func3");
  }

  public int func4(int x, int y) {
    System.out.print(String.format("func4 x=%x,y=%y",x,y));
  }    
}

Where you feed handicap 你喂残疾的地方

The only other place where I feel handicap when coming from Java is generics. 来自Java的另一个我感到有障碍的地方是泛型。 C++ has templates for that, but they some serious limitations. C ++有模板,但它们有一些严重的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM