c++ - 纯抽象类可以包含静态常量、静态方法或非虚拟析构函数吗?

标签 c++ class c++11 static abstract-class

这是一个纯抽象类吗?

class C
{
public:
    static const std::string S;
    C() {}
    virtual ~C() {}
    virtual void v() = 0;
}

我相信不是,关注this definition from WikiBooks :

A pure Abstract class has only abstract member functions and no data or concrete member functions.

它不能是纯抽象的,因为它不只有抽象的成员函数:

  1. 它有一个 virtual destructor, which is a member function ,但不抽象It is not a pure destructor .注意 "abstract method" and "pure virtual function" are the same things ,并且术语“方法”和“函数”是同义词。
  2. 它有一个代表数据的属性S

现在我的老师声称这是一个纯抽象类,因为:

Constants included in a pure virtual class are not considered attributes. They are immutable elements of a class and therefore they don't violate its abstractness. The same holds for static methods.

最佳答案

总结以上评论:

The constructor and destructor are kind-of special. The point is that they must exist for every class that can be instantiated, even if it can only be instantiated as part of a derived class' instance. The reason is that creating a derived class first constructs the base class, for which it needs a constructor. Assuming that the abstract baseclass doesn't have any data, this constructor can be trivial though.

(15 年 10 月 18 日 @ 13:46 UTC,Ulrich Eckhardt)

The C++ standard doesn't define the term "pure abstract class" at all, so it could be argued that pure abstract classes don't exist in C++; or alternatively, that you could declare a particular C++ construct to be a "pure abstract class", and that's fine as long as other participants in the discussion agree to use your definition. Just ask your teacher to spell out the precise definition of the term he or she is laboring under; nothing like a definitional argument to get one's juices flowing.

(15 年 10 月 18 日 @ 14:07 UTC,Igor Tandetnik)

The definition you provided is kinda useless in context of C++. For one thing, C++ doesn't have abstract member functions (it has pure virtual ones). A C++ class (almost) always has a constructor (possibly an implicit one), which cannot be virtual, so if you insist on counting a constructor as a member function, then no C++ class would match the definition. As to static data members, even languages that have an explicit notion of an abstract class (e.g. Java) don't disallow those. The point is that the class shouldn't have a per-instance data.

(15 年 10 月 19 日 @ 02:03 UTC,Igor Tandetnik)

关于c++ - 纯抽象类可以包含静态常量、静态方法或非虚拟析构函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33197720/

相关文章:

c++ - CreateProcess 打开加载文件的程序

c++ - 有没有办法在全局命名空间中调用类?

c++ - 是否可以在 constexpr 中使用 std::string ?

c++ - 如何删除聊天中的多余字符? C++

c++ - 未定义 vector<bool> 元素的运算符 &=?

c++ - 如何替换struct c++

javascript - 在子模块nodejs中设置父类的属性

c# - 需要一些帮助来理解继承

c++ - 相互依赖的类定义

c++ - std::chrono 和 cout