依赖于类成员属性的类实例成员
Class instance member that depends on class member attribute
我的教师为我提供了一个代表二叉树的类,我们必须用它来进行识别。 所以我正在做一个名为集群的类,事情是这样的:
BinTree(我只复制了我的东西,尽管这是最低限度的必要(:
template <typename T>
class BinTree {
public:
// Constructs an empty tree. Θ(1).
BinTree ()
: p(nullptr)
{ }
// Constructs a tree with a value x and no subtrees. Θ(1).
explicit BinTree (const T& x);
// Constructs a tree with a value x and two subtrees left and right. Θ(1).
explicit BinTree (const T& x, const BinTree& left, const BinTree& right);
簇:
class Cluster {
private:
BinTree<std::pair<std::string, double>> _cluster;
public:
Cluster();
//etc....
}
由于我不能使用继承(我们还没有达到那部分(,我真的不知道 Cluster 的构造函数会如何。集群对象将是一个二叉树,但我从"叶子"开始(英语不是我的第一语言,所以我不知道如何称呼它(,因此我必须创建一个带有字符串的集群双精度= 0.0。
我假设集群构造函数是这样的:
Cluster(const std::string& id) : _cluster(make_pair(id, 0.0)){};
这是对的吗?
然后,有 2 个特定的集群,我会将它们合并为一个。这个新的集群,因为它的_cluster属性是一个二叉树,将是以前的父级,这里是我必须使用 BinTree 的第 3 个构造函数,但我不知道该怎么做。
根据我的理解,您希望合并 2 个集群,因此,您可以像在 BinTree 类中那样完成它
。explicit Cluster ( Cluster& left, const Cluster& right){
_cluster = Cluster(left, right) ;
}
我不取消集群类的功能,直接使用 BinTree 可能更容易。
相关文章:
- 静态数据成员模板专用化的实例化点在哪里
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- C++初始化类实例时隐式调用类成员的构造函数
- 受约束的成员函数和显式模板实例化
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 访问同级类实例的成员
- static_assert私有类成员的类型,没有实例
- "new"创建的实例的所有成员变量是否都存在于堆上而不是堆栈上?
- 实例化多种类型的成员函数模板
- 清除具有已删除赋值运算符的成员的类实例
- 依赖于类成员属性的类实例成员
- 力模板静态成员实例
- C 中的成员实例如何构建
- 使用向量<向量>成员实例化对象时的隔离错误<int>
- 具有静态成员实例的单一实例
- 一个类不能有自己的静态 constexpr 成员实例吗?
- 静态成员与静态常量成员实例化 - 有什么区别?
- 使用静态成员实例的 C++ 中的线程安全单一实例(无延迟实例化)
- 显式类成员实例化
- 带有自身静态const成员实例的c++模板类