I have a class:
class node
{
public:
node& parent;
}
I want to set the parent value when I know its right value:
node parent;
...
node n; // here node.parent is a not valid value
n.parent = parent;
But I have to set it’s value in the constructor too. How can I do?
You can’t change what variable a reference references. So if you can’t initialize it in the constructor, you don’t want a reference. You can use a regular pointer, but it’s probably better to use some kind of smart pointer appropriate to your particular use. The correct answer depends primarily on how the lifetime of the referenced object is managed.