动态分配自定义数组 class 并重载运算符
Dynamically allocating array of a custom class and overloading operators
最近几天我一直在想办法为这个 class 创建一个动态分配的数组。其中一个包括我制作的另一个class
这里是 classes:
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
是一个指向 one BST 的指针,调试器这样显示它——调试器不知道它是一个指向第一个元素的指针数组。
如果您想将其视为一个数组,您需要告诉调试器这样做。
您在 "Watch" window 中执行此操作,而我 认为 显示前两个元素的语法是 t,2
。
最近几天我一直在想办法为这个 class 创建一个动态分配的数组。其中一个包括我制作的另一个class
这里是 classes:
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
是一个指向 one BST 的指针,调试器这样显示它——调试器不知道它是一个指向第一个元素的指针数组。
如果您想将其视为一个数组,您需要告诉调试器这样做。
您在 "Watch" window 中执行此操作,而我 认为 显示前两个元素的语法是 t,2
。