构造一个内部有结构的类 c++
所以我有一个类,它在其私有变量内保存一个结构,在这个结构内我有一个数组,其中数组的大小仅在类构造后确定。
template <typename T>
class btree {
public:
btree(size_t maxNodeElems);
~btree() {}
private:
// The details of your implementation go here
size_t maxNodeElems;
struct node {
list <T> elements;
node lvl[];
};
};
首先,我是否必须将其设置为它的 node * lvl
以及如何调用该结构内的变量?它与私有变量相同吗?所以每当我在 btree 类的函数之一中使用它时,我都可以将其称为 btree.lvl
或者是 btree->node->lvl
还是有特殊的方法来做到这一点?
另外,我的数组必须是 maxNodeElems+1 如果有人可以帮助我,我将不胜感激!
So I have a class which holds a struct inside its private variables and inside this struct I have an array where the size of the array is only determined after the construction of the class.
template <typename T>
class btree {
public:
btree(size_t maxNodeElems);
~btree() {}
private:
// The details of your implementation go here
size_t maxNodeElems;
struct node {
list <T> elements;
node lvl[];
};
};
Firstly, do I have to make it so its node * lvl
and how do I call the variables inside this struct? Is it the same as a private variable, so whenever I use it inside one of the functions in btree class
I can call it be btree.lvl
or is it btree->node->lvl
or is there a special way to do this?
Also, my array has to be of maxNodeElems+1
if someone can help me, that'd be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只是声明类型,而不是该类型的实际对象。您需要将结构声明设为公开,将对象设为私有:
您可以从外部创建该类型的对象:
为了访问成员,您可以使用公共 getter 和 getter。 btree 类中的设置器:
编辑:
以下内容对我有用(初始化成员):
You are just declaring the type, not an actual object of that type. You need to make your struct declaration public and the object private:
You can create objects of that type from outside:
For accessing members, you can have public getters & setters in your btree class:
EDIT:
The following works for me (initializing the member):