문제

I'll describe my question using an example. I have the structure Triangle:

struct Triangle {
int perimeter;
double area;
list <mystruct> sides;
};

where mystruct is:

struct mystruct {
int length;
}

Is it possible? Are there any problems that may appear?

도움이 되었습니까?

해결책

Yes, it's possible. In fact it's a composition. You can use it like this:

mystruct s;
s.length = 10;

Triangle t;
t.sides.push_back(s);

object composition is a way to combine simple objects or data types into more complex ones.

다른 팁

No problem at all.

std::list<> is well written/implemented and is copyable (even moveable), default constructible, and so on, so you can use it in a struct without fearing anything.

The only significant difference between struct and class for nearly all purposes is the default access specifier.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top