gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:24:50 28 4
gpt4 key购买 nike

这是一个纯抽象类吗?

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com