c++ - 为什么要使用抽象类来创建类库?

标签 c++ oop

When and Why to use abstract classes/methods?

When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.) Use an abstract class to define a common base class for a family of types. Use an abstract class to provide default behavior. Subclass only a base class in a hierarchy to which the class logically belongs.

我没看懂上面引用的解释。请解释为什么要使用抽象类来创建类库?

最佳答案

你应该非常仔细地阅读这类文章。据我所知,文章的主要部分是专门介绍 c# 语言的。就这种语言而言,接口(interface)和抽象类之间存在很大差异。例如,c# 语言中的接口(interface)只是一组“纯”虚方法(它们不能在接口(interface)内定义,只有类才能实现它)。接口(interface)不能有构造函数。抽象类可以有构造函数。此外,c# 不支持多重继承(与 c++ 语言相反)。这样,C# 接口(interface)和抽象类看起来与 C++ 的接口(interface)和抽象类非常不同

关于c++ - 为什么要使用抽象类来创建类库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42970147/

相关文章:

c++ - 这个 std::string 构造函数是什么意思

c++ - "Reparent"来自编译代码的类

c++ - 哎呀概念-得到意想不到的结果

C++ 继承和成员函数指针

c++ - 节点的迭代层次结构 - 访客和复合?

JavaFx:如何使立方体适应窗口大小调整

c++ - 计算满足xy<c的正整数对的个数

c++ - Boost Log Scoped Log/Attributes 未显示

c++ - 从简单的 arrayfire 构造函数中获取段错误

java - 使用命令设计模式