The following doesn't work as desired (print 2) because, I guess, the nodes are passed by value even though the vector is passed by reference. How could I fix it?
#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;
class Node{
public:
int value;
Node(int);
void createChildren(vector<Node> &);
};
//! constructor of a single node
Node::Node(int value)
{
this->value = value;
}
void Node::createChildren(vector<Node> &nodes)
{
for (int i = 0; i < 5; i++) {
Node n(0);
nodes.push_back(n);
if (i == 0) {
value = nodes.size();
}
}
}
int main(void) {
Node a(0);
vector<Node> asdf;
asdf.push_back(a);
asdf[0].createChildren(asdf);
cout << asdf[0].value << endl;
return 0;
}