c ++类可以将自身包含为成员吗? [英] Can a c++ class include itself as an member?
问题描述
我试图通过用C ++编写然后使用ctypes或cython来加快python例程的速度。
I'm trying to speed up a python routine by writing it in C++, then using it using ctypes or cython.
我是c ++的新手。我正在免费使用Microsoft Visual C ++ Express。
I'm brand new to c++. I'm using Microsoft Visual C++ Express as it's free.
我计划实现一个表达式树,以及一种按后缀顺序对其求值的方法。
I plan to implement an expression tree, and a method to evaluate it in postfix order.
我立即遇到的问题是:
class Node {
char *cargo;
Node left;
Node right;
};
我不能声明 left
或 right
作为 Node
类型。
I can't declare left
or right
as Node
types.
推荐答案
否,因为对象将无限大(因为每个 Node
都有两个其他 Node
作为成员对象,每个对象都有两个其他 Node
对象,每个对象……嗯,您明白了)。
No, because the object would be infinitely large (because every Node
has as members two other Node
objects, which each have as members two other Node
objects, which each... well, you get the point).
但是,您可以将指向类类型的指针作为成员变量:
You can, however, have a pointer to the class type as a member variable:
class Node {
char *cargo;
Node* left; // I'm not a Node; I'm just a pointer to a Node
Node* right; // Same here
};
这篇关于c ++类可以将自身包含为成员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!