سؤال

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