Skip to content

Why inheritance and not composition? #45

@abin-z

Description

@abin-z
template <typename Comparator>
class IniSectionBase : public std::map<std::string, IniField, Comparator>
{
  // ...
}

It's great to see such a great open source library!
But I have a question: Inheriting standard library containers is generally not recommended. The recommended practice is to compose.

Reason: None of the STL containers are designed to be used as base classes, so they do not have virtual destructors defined; this may lead to resource leaks if you point to a dynamically-allocated derived class object with a pointer to the base class and delete the pointer!

Are there any considerations for using inheritance instead of composition in your open source library?

I hope to hear from you.

很高兴看到如此优秀的开源库!
但是我有一个疑问: 通常来说不推荐继承标准库容器. 推荐的做法是组合.

原因: 所有的 STL 容器都未被设计为作为基类来使用,因此它们都没有定义虚析构函数;这样如果你用基类指针指向动态分配的派生类对象并 delete 该指针可能会导致资源泄漏!

你的开源库使用继承而不是组合是有什么考量吗?

希望能得到你的回复!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions