动态分配自定义类的数组和重载运算符
Dynamically allocating array of a custom class and overloading operators
在过去的几天里,我一直在试图弄清楚如何为我制作的这个类制作一个动态分配的数组。其中一个包括我制作的另一个课程
以下是课程:
template<typename T>
class BST // a binary search tree class
{
private:
Node<T> * root;
int size;
public:
BST()
{
root = NULL;
size = 0;
}
/*void * BST::operator new[](size_t a) //tried it both with the "BST::" and without
{
void * p = malloc(a);//24
return p;
}*/
//there are more functions that i cut out
}
和
template <typename T>
class Node //a node for a binary search tree
{
public:
int key;
T data;
Node * left;
Node * right;
Node * parent;
//public:
Node<T>()
{
key = NULL;
data = NULL;
left = NULL;
right = NULL;
parent = NULL;
}
//more stuff below this but it's just getting and setting the data
}
在main()
中,我将尝试使用以下行创建BST
对象的数组:
BST<char> * t = new BST<char>[ n ]; //the user will give the value for int n
问题是它在运行时只创建一个BST
对象。我做了一些研究并尝试重载 new[] 运算符,它完全没有做任何事情。
有人可以解释一下这样做的正确方法是什么吗?
数组中确实有多个对象,但t
不是数组。
t
是指向一个BST 的指针,调试器会这样显示它 – 调试器不知道它是指向数组第一个元素的指针。
如果要将其视为数组,则需要告诉调试器执行此操作。
您在"监视"窗口中执行此操作,我认为语法对于显示前两个元素t,2
。
相关文章:
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 在 myVector 类中重载运算符 + 时出错
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 如何在 cpp 中重载运算符 +=?
- C++ 如何重载 [] 运算符并进行函数调用
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 重载运算符有地址吗?
- 如何迭代重载运算符 [] 的类?
- 重载运算符与添加问题
- 模板基类中的重载运算符
- 如何调用用于重载运算符"<<"的 friend 函数?
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- 重载运算符<<采用谷歌 C++ 风格
- C++ 如何正确重载 + 运算符
- cout (<<) 重载运算符不打印减去的矩阵
- 如何在 c++ 中重载运算符 + 以便能够 whrite c_str = "smth" + c_str;
- 重载运算符*以获取对另一个类的实例的引用