C ++ - 这是什么双指针/阵列结构究竟意味着什么?我有麻烦想象它 [英] C++ - What does this double pointer/array structure really mean? I have trouble picturing it

查看:78
本文介绍了C ++ - 这是什么双指针/阵列结构究竟意味着什么?我有麻烦想象它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C ++ code在

This is within C++ code

YInterface ** Y_list;

int numberofYInterfaces;

然后

numberofYInterfaces=1;

Y_list = new YInterface *[numberofYInterfaces];        //What ??????

我有一个非常艰难的时间描绘Y_list的结构?然后想象我困惑的时候,如果numberofYInterfaces将成为多个,甚至2?

I have a very hard time picturing the structure of Y_list? And then imagine my confusion when and if the numberofYInterfaces becomes more than one, even 2?

OOPS 对不起,我忘了写下面的语句:

OOPS sorry and I forgot to write this below statement:

Y_list[i]= new AnotherClass();

//以后除了我的原单的问题。感谢您的headsup埃里克·福尔廷。
注:AnotherClass从YInterface继承。

//later addition to my orginal question . Thanks for the headsup Eric Fortin. Note: AnotherClass is inherited from YInterface.

任何帮助是pciated AP $ P $

Any help is appreciated

推荐答案

在声明 Y_list ,你得到的是一个未确定的值一个变量:

When you declare Y_list, you get a single variable with an unspecified value in it:


Y_list
+-------+
|       |
+-------+

当您使用新[] ,它指向任何新[] 分配,在这种分配给它案例是一个指针数组:

When you assign it with new[], it points to whatever new[] allocated, which in this case is an array of pointers:


Y_list         array
+-------+     +-------+
|   o---+---->|       |
+-------+     +-------+
              |       |
              +-------+
              |       |
              +-------+
              |       |
              +-------+
              |       |
              +-------+

在这一点上,你还没有任何 YInterface 的对象。你刚才已分配空间来保存的指针的他们,要他们曾经存在。

At that point, you don't yet have any YInterface objects. You just have space allocated to hold pointers to them, should they ever exist.

最后,你分配一个值的元素 Y_list [I] =新AnotherClass 之一。这是当一个 YInterface 后裔存在,但只有一个至今。运行code在一个循环,你会得到多个实例。但是,你仍然只是有一个数组,一个指针数组。

Finally, you assign a value to one of the elements Y_list[i] = new AnotherClass. That's when a YInterface descendant exists, but you only have one so far. Run that code in a loop and you'll get multiple instances. But you still just have one array, and one pointer to that array.


Y_list         array         AnotherClass
+-------+     +-------+     +-------+
|   o---+---->|   o---+---->|       |
+-------+     +-------+     |       |
              |       |     |       |
              +-------+     |       |
              |       |     |       |
              +-------+     +-------+
              |       |
              +-------+
              |       |
              +-------+

要释放所有这一切,记得有一个删除语句你有每个语句,一个删除[] 每个新[]

To free all this, remember to have one delete statement for each new statement you had, and one delete[] for each new[]:

delete Y_list[i];

delete[] Y_list;

这篇关于C ++ - 这是什么双指针/阵列结构究竟意味着什么?我有麻烦想象它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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