C++本身的结构?
我一直在尝试将此代码移植到 python 中,但在 C++ 中有一些我不太理解的东西(我确实知道一点 C++,但这超出了我的范围):
typedef struct huffnode_s
{
struct huffnode_s *zero;
struct huffnode_s *one;
unsigned char val;
float freq;
} huffnode_t;
我不明白的是 huffnode_s 是如何在其中的就其本身而言,我以前从未见过这一点,也不太明白。这是什么意思?如果有人可以的话,Python 的等价物是什么?
I've been trying to port this code to python, but there is something I do not quite understand in C++ (I do know a bit of C++ but this is beyond me):
typedef struct huffnode_s
{
struct huffnode_s *zero;
struct huffnode_s *one;
unsigned char val;
float freq;
} huffnode_t;
What I don't get is how huffnode_s can be within itself, I've never seen this before and don't quite understand it. What does this mean, and if someone can, what would be the python equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
huffnode_s
本身并不存在,只有指向huffnode_s
的指针位于其中。由于指针的大小已知,因此没有问题。huffnode_s
isn't within itself, only pointers tohuffnode_s
are in there. Since a pointer is of known size, it's no problem.这。
然后,您可以将各种 C 函数重构为此类的方法。
或者也许是这个。
如果您希望 C 函数保持函数状态。
就是这样。
这就是所需要的全部。
This.
You can then refactor your various C functions to be methods of this class.
Or maybe this.
If you want your C functions to remain functions.
That's it.
That's all that's required.
它本身没有结构。它有一个指向该结构的指针。
在内存中 struct huffnode_s 看起来像(32 位机器):
|-------------------- huffnode_s* 零 - 4 字节 ----- ---------|
|------------------ huffnode_s* 1 - 4 字节----------------|
|unsigned char val - 1 字节 + 3 字节填充=======|
|------------------- 浮点频率 - 4 字节 ------------------------- |
这些大小会因机器而异,并且它在内存中的外观由编译器决定。
it does not have a structure in itself. it has a pointer to that structure.
in memory struct huffnode_s would look like (32 bit machine):
|------------------ huffnode_s* zero - 4 bytes --------------|
|------------------ huffnode_s* one - 4 bytes----------------|
|unsigned char val - 1 byte + 3 bytes padding=======|
|------------------- float freq - 4 bytes -------------------------|
these sizes would vary machine to machine, and how it looks in memory is decided by compiler .
为了补充 Carl 的答案,在 C++ 中同样的事情也是可能的:(
注意上面的类很愚蠢,但重点是你可以在类类型的类中拥有一个指针)
To add to Carl's answer, the same thing in C++ is also possible:
(Note the above class is silly, but the point is you can have a pointer inside a class that is of the class type)
(struct huffnode_s *) 声明一个指向另一个结构的指针,该结构包含与其在其中声明的结构相同的变量。请参阅
(struct huffnode_s *) declares a pointer to another structure that includes same variables as the structure that it's declared in. See this question.
这是一个指向 huffnode 内部的 huffnode 的指针。这意味着你可以说:
这将编译,并且只要所有这些 huffnode 后代实际上都被正确分配和指向,它就会工作。
指针很像 JavaScript 中的对象引用。它们实际上并不包含数据,它们只是引用它。请放心,您看到的不是无限类型。
This is a pointer to a huffnode inside of a huffnode. What this means is that you can say:
This will compile, and it will work as long as all those huffnode descendents are actually allocated and pointed to correctly.
Pointers are much like object references in JavaScript. They don't actually contain data, they just refer to it. Rest assured that you are not looking at an infinite type.
这称为自引用结构,正如它听起来的那样:包含对自身引用的结构。这种情况常见于描述链表节点的结构中。每个节点都需要对链中下一个节点的引用。
This is known as a self referential structure and it is exactly what it sounds like: a structure which contains a reference to itself. A common occurrence of this is in a structure which describes a node for a linked list. Each node needs a reference to the next node in the chain.
正如其他人所指出的,对自身的引用只是指向该结构的其他实例的指针。
结构内的指针允许将实例作为链接列表连接在一起。
As others have noted, the references to itself are simply pointers to other instances of that structure.
The pointers within the structure would allow one to connect instances together as a linked list.