addChild() sollte typisierte non-owning Pointer zurückgeben, nicht size_t #6

Closed
opened 2026-04-16 19:42:42 +00:00 by sebastian · 0 comments
Owner

Problem: addChild() gibt aktuell size_t zurück. Um später auf das Kind zuzugreifen, braucht man getChildAtIndex(id) + dynamic_cast:

inventoryContainerID = uiContainer->addChild(std::move(inv));
// später:
auto* c = dynamic_cast<UiInventoryContainer*>(rootContainer->getChildAtIndex(inventoryContainerID));

Lösung:

template<typename T>
T* addChild(std::unique_ptr<T> child) {
    T* ptr = child.get();
    children.push_back(std::move(child));
    child->parent = this;
    return ptr;  // non-owning, Ownership bleibt bei children
}
Problem: addChild() gibt aktuell size_t zurück. Um später auf das Kind zuzugreifen, braucht man getChildAtIndex(id) + dynamic_cast: ``` inventoryContainerID = uiContainer->addChild(std::move(inv)); // später: auto* c = dynamic_cast<UiInventoryContainer*>(rootContainer->getChildAtIndex(inventoryContainerID)); ``` Lösung: ``` template<typename T> T* addChild(std::unique_ptr<T> child) { T* ptr = child.get(); children.push_back(std::move(child)); child->parent = this; return ptr; // non-owning, Ownership bleibt bei children } ```
sebastian added the
refactoring
engine
labels 2026-04-16 19:42:42 +00:00
sebastian added this to the Quick Wins milestone 2026-04-16 19:50:26 +00:00
sebastian added this to the Dicewars project 2026-04-16 20:59:29 +00:00
sebastian started working 2026-04-17 06:09:08 +00:00
sebastian stopped working 2026-04-17 06:34:30 +00:00
25 minutes 22 seconds
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Total Time Spent: 25 minutes 22 seconds
sebastian
25 minutes 22 seconds
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sebastian/Dicewars-Siedler#6
No description provided.