简体   繁体   English

C ++中是否有纯抽象类?

[英]Are there pure abstract classes in C++?

There is a homework question which asks what construct in C++ is similar to a Java interface. 有一个作业问题,询问C ++中的哪种构造类似于Java接口。 An interface in Java can be referred to as a pure abstract class, and I know that C++ has abstract classes, but are pure abstract classes something that C++ officially has ? Java中的接口可以称为纯抽象类,我知道C ++具有抽象类,但是C ++正式具有纯抽象类吗?

Maybe from a C++ designer's viewpoint it doesn't, but technically it is possible to create an pure abstract class in C++ by making all methods abstract right? 也许从C ++设计人员的角度来看不是这样,但是从技术上讲,可以通过使所有方法抽象正确来在C ++中创建纯抽象类?

I looked at this resource but I'm still confused after reading some of the answers... 我看了这个资源,但在阅读了一些答案后仍然感到困惑...

Yes you can create an abstract class in c++ 是的,您可以在C ++中创建一个抽象类

class A {

public:
    A() {};
    virtual ~A(){};
    virtual void temp() = 0;
};

int main () {
    A a; // compiler error
}
class Foo {
public:
    Foo();
    virtual ~Foo() {};
    virtual void bar() = 0;
}

Foo is a pure abstract class in C++ because it contains the method bar() which is a pure virtual method. Foo是C ++中的纯抽象类,因为它包含方法bar() ,它是纯虚拟方法。

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

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