अब तक मैं सिर्फ यूनियनों का इस्तेमाल किया है मैं अब इस मामले में जहां मैं परिवर्तन क्रम के दौरान इस्तेमाल सदस्य चाहते हैं, उसमें अपने आप को मिल रहा है या तो सदस्य एक या सदस्य बी
स्टोर करने के लिए।
सी ++ संघ उपयोग
union NextGen {
std::shared_ptr<TreeRecord> Child = nullptr;
std::vector<std::shared_ptr<TreeRecord>> Children;
};
मेरे वर्तमान उपयोग:
void TreeRecord::AddChild(const std::shared_ptr<TreeRecord>& NewChild) {
if(_childCount == 0) {
_nextGeneration.Child = NewChild;
_childCount++;
} else if(_childCount == 1) {
//This is not clear to me:
//Do I have to set Child to nullptr first?
//Do I need to clear the Children vecor?
//Or will it work like this?
_nextGeneration.Children.push_back(_nextGeneration.Child);
_nextGeneration.Children.push_back(NewChild);
_childCount++;
} else {
_nextGeneration.Children.push_back(NewChild);
_childCount++;
}
}
नए सिरे से कार्यान्वयन (कोशिश):
typedef std::shared_ptr<TreeRecord> singlechild_type;
typedef std::vector<std::shared_ptr<TreeRecord>> children_type;
union {
singlechild_type _child;
children_type _children;
};
void TreeRecord::AddChild(const singlechild_type& NewChild) {
if(_childCount == 0) {
_child = NewChild;
_childCount = 1;
} else if(_childCount == 1) {
singlechild_type currentChild = _child; //Copy pointer
_child.~singlechild_type(); //Destruct old union member
new (&_children) children_type(); //Construct new union member
_children.push_back(currentChild); //Add old child to vector
_children.push_back(NewChild); //Add new child to vector
_childCount = 2;
} else {
_children.push_back(NewChild);
_childCount++;
}
}
इसके बारे में भी सोचा नहीं था! "सामान्य में" –
का आपका क्या मतलब है यह सही जवाब है, लेकिन यदि आप विस्तृत करते हैं तो यह अच्छा होगा। – mostruash
मेरे पास एक elem वेक्टर नहीं होगा। मैं या तो 0 या 1 elem के लिए चाइल्ड का उपयोग करता हूं और मैं 1 से अधिक elems के लिए वेक्टर पर स्विच करता हूं या मैंने गलती की है ...? (मैं अभी भी आपका कोड पढ़ रहा हूं) और मैं एक टैग किए गए यूनियन का उपयोग कर रहा हूं, सिर्फ बुलियन पहचानकर्ता के साथ नहीं बल्कि "size_t _childCount" के साथ, जो 0 या 1 है जब बच्चा मधुमक्खी का उपयोग कर रहा है। –