我可以在 C++ 类中使用 `abstract` 关键字吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1298093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:28:05  来源:igfitidea点击:

Can I use `abstract` keyword in C++ class

c++keyword

提问by DevSolar

Can we write abstract keyword in C++ class?

我们可以在 C++ 类中编写抽象关键字吗?

回答by Ken

#define abstract

回答by DevSolar

No.

不。

Pure virtual functions, in C++, are declared as:

在 C++ 中,纯虚函数被声明为:

class X
{
    public:
        virtual void foo() = 0;
};

Any class having at least one of them is considered abstract.

任何至少具有其中之一的类都被认为是抽象的。

回答by Martin v. L?wis

No, C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes.

不,C++ 没有关键字抽象。但是,您可以编写纯虚函数;这就是 C++ 表达抽象类的方式。

回答by rahul

It is a keyword introduced as part of the C++/CLI language spefication for the .NET framework.

它是作为 .NET 框架的 C++/CLI 语言规范的一部分引入的关键字。

回答by Svetlozar Angelov

no, you need to have at least one pure virtual function in a class to be abstract.

不,你需要在一个类中至少有一个纯虚函数才能抽象。

Here is a good reference cplusplus.com

这是一个很好的参考cplusplus.com

回答by anorm

As others point out, if you add a pure virtual function, the class becomes abstract.

正如其他人指出的那样,如果您添加一个纯虚函数,该类就会变得抽象。

However, if you want to implement an abstract base class with no pure virtual members, I find it useful to make the constructor protected. This way, you force the user to subclass the ABC to use it.

然而,如果你想实现一个没有纯虚拟成员的抽象基类,我发现让构造函数受保护是很有用的。这样,您就可以强制用户对 ABC 进行子类化以使用它。

Example:

例子:

class Base
{
protected:
    Base()
    {
    }

public:
    void foo()
    {
    }

    void bar()
    {
    }
};

class Child : public Base
{
public:
    Child()
    {
    }
};

回答by Biedrzyk

actually keyword abstractexists in C++ (VS2010 at least) and I found it can be used to declare a class/struct as non-instantiated.

实际上关键字abstract存在于 C++(至少 VS2010)中,我发现它可以用来将类/结构声明为非实例化。

struct X abstract {
    static int a;
    static void foX(){};
};
int X::a = 0;
struct Y abstract : X { // something static
};
struct Z : X { // regular class
};
int main() {
    X::foX();
    Z Zobj;
    X Xobj;    // error C3622
}

MSDN: https://msdn.microsoft.com/en-us/library/b0z6b513%28v=vs.110%29.aspx

MSDN:https: //msdn.microsoft.com/en-us/library/b0z6b513%28v=vs.110%29.aspx

回答by Varun Kumar

No, you can't use abstract as a keyword because there is no such keyword available in C++.

不,您不能使用 abstract 作为关键字,因为 C++ 中没有这样的关键字。

If you want make a class as an in C++ abstract you can declare at least one function as pure virtual function.

如果你想在 C++ 中创建一个抽象类,你可以将至少一个函数声明为纯虚函数。

But in derived class you must provide definition else its give compilation error .

但是在派生类中,您必须提供定义,否则会出现编译错误。

Example:

例子:

class A
{
public:
  virtual void sum () = 0;
};

note:

笔记:

You can used abstract as a variable name, class name because, as I told you, abstract is not a keyword in C++.

您可以将抽象用作变量名、类名,因为正如我告诉您的,抽象不是 C++ 中的关键字。

回答by Varun Kumar

No, C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes. It is a keyword introduced as part of the C++/CLI language spefication for the .NET framework. You need to have at least one pure virtual function in a class to be abstract.

不,C++ 没有关键字抽象。但是,您可以编写纯虚函数;这就是 C++ 表达抽象类的方式。它是作为 .NET 框架的 C++/CLI 语言规范的一部分引入的关键字。你需要在一个类中至少有一个纯虚函数才能成为抽象的。

class SomeClass {
public:
   virtual void pure_virtual() = 0;  // a pure virtual function
};

回答by Anoop Toffy

There is no keyword 'abstract' but a pure virtual function turns a class in to abstract class which one can extend and re use as an interface.

没有关键字“抽象”,但纯虚函数将类转换为抽象类,可以扩展并重新用作接口。