简体   繁体   English

remove()STL多继承的处理

[英]remove() Handling of STL Multi-Inheritance

We have created three STL and STL-inherited data types for use in a clustering algorithm: 我们创建了三种STL和STL继承的数据类型以用于聚类算法:

typedef std::vector<double>Point;   // A list of parameters (a single observation)

struct Cluster : std::list<Point> {   // A list of Points
   // Additional member variables
   Point centroid;
   bool centroid_valid;
   bool sort_valid;
   // Cluster functions omitted
};

struct Universe : std::list<Cluster> {   // A list of Clusters
   // No member variables
   // Universe functions omitted
};

This is an attempt to fully utilize the STL so nothing is new 'd into existence. 这是一种尝试充分利用STL的尝试,因此没有new东西存在。 The concern is having to do with the nature of subordinated STL functionality, specifically: 关注点与从属STL功能的性质有关,尤其是:

If we were to remove() a Cluster element from a Universe list, would the STL handle not only the deletion (and memory management) of all Point s in the Cluster to be removed, but also the deletion-handling of all member variables? 如果我们要从Universe列表中remove()一个Cluster元素,那么STL不仅要处理要删除的Cluster中所有Point的删除(和内存管理),还要处理所有成员变量的删除处理?

Note: all member functions are relatively simple, with no static operations. 注意:所有成员函数都相对简单,没有静态操作。

If we were to remove() a Cluster element from a Universe list, would the STL handle not only the deletion (and memory management) of all Points in the Cluster to be removed, but also the deletion-handling of all member variables? 如果我们要从Universe列表中删除()一个Cluster元素,那么STL不仅会处理要删除的Cluster中所有点的删除(和内存管理),而且还会处理所有成员变量的删除处理吗?

Yes it will because it operates on Cluster objects and removing it will remove all its fields. 是的,因为它将对群集对象进行操作,将其删除将删除其所有字段。 But inheritance from standard containers is considered to be a bad practice because they do not have a virtual destructor. 但是,从标准容器继承是一种不好的做法,因为它们没有虚拟析构函数。 It is better to store list of Points in a Cluster as a property. 最好将群集中的点列表存储为属性。

wrong: 错误:

typedef std::vector<double>Point;   // A list of parameters (a single observation)

struct Cluster : std::list<Point> {   // A list of Points
   // Additional member variables
   Point centroid;
   bool centroid_valid;
   bool sort_valid;
   // Cluster functions omitted
};

right: 对:

typedef std::vector<double>Point;   // A list of parameters (a single observation)

struct Cluster {   // A list of Points

   std::list<Point> points;

   // Additional member variables
   Point centroid;
   bool centroid_valid;
   bool sort_valid;
   // Cluster functions omitted
};

When you inherit from a class, you are inherently guaranteeing that all of its member functions will behave sanely when called against your class. 当您从类继承时,本质上是在保证对您的类调用该类的所有成员函数时,它们的行为都将合理。

The problem for you is that the 'additional member variables' are cached observations of the points. 您遇到的问题是“其他成员变量”是对点的缓存观察。 If you erase or insert a point, these cached observations will be out of sync, and therefore wrong . 如果eraseinsert点,则这些缓存的观测值将不同步,因此是错误的

If you want Cluster to have the same interface as std::list, then you must implement that interface, call into the member points and update (or mark as invalid) the cached observations. 如果希望Cluster具有与std :: list相同的接口,则必须实现该接口,调用成员points并更新(或将其标记为无效)缓存的观察值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM