如何在C ++中动态分配指针数组? [英] How to dynamically allocate an array of pointers in C++?

查看:109
本文介绍了如何在C ++中动态分配指针数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程

class Node
{
    int key;
    Node**Nptr;
public:
    Node(int maxsize,int k);
};
Node::Node(int maxsize,int k)
{
   //here i want to dynamically allocate the array of pointers of maxsize
   key=k;
}

请告诉我如何在构造函数中动态分配指针数组- -此数组的大小为maxsize。

Please tell me how I can dynamically allocate an array of pointers in the constructor -- the size of this array would be maxsize.

推荐答案

Node::Node(int maxsize,int k)
{
   NPtr = new Node*[maxsize];
}

但是像往常一样,您最好使用std :: vector指针。

But as usual, you are probably better off using a std::vector of pointers.

这篇关于如何在C ++中动态分配指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆