我将用一个例子来描述我的问题。我有结构三角形:
struct Triangle {
int perimeter;
double area;
list <mystruct> sides;
};
其中 mystruct 是:
struct mystruct {
int length;
}
可能吗?有没有可能出现的问题?
是的,这是可能的。事实上,它是一个组合。你可以像这样使用它:
mystruct s;
s.length = 10;
Triangle t;
t.sides.push_back(s);
对象组合是一种将简单对象或数据类型组合成更复杂对象的方法。
几乎所有用途之间的唯一显着区别是默认访问说明struct
符。class
完全没有问题。
std::list<>
编写/实现良好,可复制(甚至可移动),默认可构造等,因此您可以在 a 中使用它struct
而不必担心任何事情。