Alright folks! gonna use const Node* getChild(int index) const for now instead of getChildren() to get the whole thing. I literally have no use of it for iteration whatsoever outside itself anyway.
As Oberon_Command said, that means you won't be able to edit the child.
Normally what is done in these situations, is to provide two functions:
const Node* getChild(int index) const //Called when the class is const (as accessed through whatever variable you are using to call the function)
Node* getChild(int index) //Called when the class isn't const
That way, when you do something like this:
MyClass myClass;
Node *node = myClass.getChild(12); //Calls the non-const version of the function, which returns a non-const node.
if(node) node->setValue("meow"); //And we can read or write to non-const nodes.
...you can change the node's values.
Same thing if you are using a non-const reference or pointer:
MyClass &myClassRef = myClass;
Node *node = myClassRef.getChild(12);
if(node) node->setValue("meow");
But when you are trying to access the function through a const variable or const reference, it prevents writing:
const MyClass &constRef = myClass;
const Node *node = constRef.getChild(12); //Calls the const version of the function, which returns a const node pointer.
if(node)
{
node->setValue("meow"); //Properly fails to compile, since we don't want to allow editing of const classes.
node->getValue(); //We can still read from the class though.
}
And if you do need iteration, you can just create begin() / end() functions in the class yourself, by exposing the internal container's begin() / end().
If you're using a modern C++14 compiler, it's as easy as:
class MyClass
{
public:
auto begin() { return children.begin(); }
auto begin() const { return children.begin(); } //We want const and non-const iteration to be available.
auto end() { return children.end(); }
auto end() const { return children.end(); }
};
Note: If you are using an older compiler (C++11 or C++03), it's still easy to do, it just requires a smidgen more boilerplate code.