Why Node or better object isnt a referernce? (c++)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Charles Woodhill
:warning: Old Version Published before Godot 3 was released.

Hi again :slight_smile:

Why are nodes/object not implement/inherit from reference?
i think there is a reason for that…

ive noticed when nodes removed from tree/parent then they not destructed,
which is ok i expect that.
but why they no reference?

for example, i dynamically build a gui control at runtime, with rows.
each row has n controls.
so now i have to keep an array (or similar) to store the controls (for later destruction)

wouldnt references make this much more simpler?
or have i missed something?

p.s. i come from c# where everything is a reference :slight_smile:

EDIT: ok i could loop over childs and free all, maybe bad example, but question still vaid :slight_smile:

greets & tnx :slight_smile:

You don’t need to manually free all the children if you free their parent, because then children are automatically freed as well.

raymoo | 2017-03-16 17:08

:bust_in_silhouette: Reply From: Zylann

Nodes are not references mainly because their ownership is managed by the tree. As raymoo said, deleting a node deletes its children. There are not much use cases for nodes to be references, even if it sounds “magically handy”, but when you think about it, you also avoid the overhead of refcounts.
As a result it also needs extra caution, especially in C++.