构造一个内部有结构的类 c++

发布于 2024-12-11 06:44:33 字数 592 浏览 0 评论 0原文

所以我有一个类,它在其私有变量内保存一个结构,在这个结构内我有一个数组,其中数组的大小仅在类构造后确定。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-12-18 06:44:33

您只是声明类型,而不是该类型的实际对象。您需要将结构声明设为公开,将对象设为私有:

template <typename T> 
class btree {

  public:
    btree(size_t maxNodeElems);
    ~btree() {}

    struct node {   // <- this is just a declaration of a private inner-class
      list <T> elements;
      node lvl[];
    };

  private:
    size_t maxNodeElems;
    node*  memberNode;   // <- this is the actual private member

};

您可以从外部创建该类型的对象:

btree<A>::node* n = new btree<A>::node;

为了访问成员,您可以使用公共 getter 和 getter。 btree 类中的设置器:

class btree {
public:
   node* getNode()
   {
      return memberNode;
   }
   //...........
   //...........
};

编辑:

以下内容对我有用(初始化成员):

template <typename T> 
class btree {

  public:
    btree()
    {
       memberNode = new btree<T>::node;
    }
    ~btree() {}

    void init()
    {
       memberNode->lvl = new node[10];
    }

    struct node {   // <- this is just a declaration of a private inner-class
      list <T> elements;
      node* lvl;
    };

  private:
    size_t maxNodeElems;
    node*  memberNode;   // <- this is the actual private member

};

int _tmain(int argc, _TCHAR* argv[])
{
   btree<char> b;
   b.init();
}

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:

template <typename T> 
class btree {

  public:
    btree(size_t maxNodeElems);
    ~btree() {}

    struct node {   // <- this is just a declaration of a private inner-class
      list <T> elements;
      node lvl[];
    };

  private:
    size_t maxNodeElems;
    node*  memberNode;   // <- this is the actual private member

};

You can create objects of that type from outside:

btree<A>::node* n = new btree<A>::node;

For accessing members, you can have public getters & setters in your btree class:

class btree {
public:
   node* getNode()
   {
      return memberNode;
   }
   //...........
   //...........
};

EDIT:

The following works for me (initializing the member):

template <typename T> 
class btree {

  public:
    btree()
    {
       memberNode = new btree<T>::node;
    }
    ~btree() {}

    void init()
    {
       memberNode->lvl = new node[10];
    }

    struct node {   // <- this is just a declaration of a private inner-class
      list <T> elements;
      node* lvl;
    };

  private:
    size_t maxNodeElems;
    node*  memberNode;   // <- this is the actual private member

};

int _tmain(int argc, _TCHAR* argv[])
{
   btree<char> b;
   b.init();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文