Wie optionale Member definieren?
-
Mahlzeit!
Ich habe eine class Section, die IMMER eine WidthSection sowie eine OPTIONALE InfoSection besitzt. Wie kann ich das am besten ausdrücken?
Idee 1: Per Zeiger (wenn keine InfoSection, ist mInfoSection = NULL)
class Section { WidthSection mWidthSection; InfoSection* mInfoSection; };
Idee 2: Per bool (wenn keine InfoSection, ist mHasInfoSection = false)
class Section { WidthSection mWidthSection; InfoSection mInfoSection; bool mHasInfoSection };
Was fändet ihr besser? Oder gibts in C++ einen noch besseren Ansatz?
-
boost::optional<InfoSection>
und in Zukunft
std::optional<InfoSection>
-
Wieso nicht Vererbung? Section <- InfoSection
-
out schrieb:
Wieso nicht Vererbung? Section <- InfoSection
Weil es keinen Sinn ergibt. Es ist eine Komposition und keine Vererbung...
@Optionalitätsinterferenz: Danke! Das sieht schon mal sehr gut aus!
-
unique_ptr<InfoSection>
.
-
Arcoth schrieb:
unique_ptr<InfoSection>
.Das braucht Heap Memory, optional nicht zwingend.
-
Schon klar. Ich wollte nur eine zusaetzliche Option vorschlagen.