I have the requirement to contain a pointer of same class within a class ( something like link list , containing the pointer to next link ). What my requirement is something like this :
class A
{
class A* child;
}
I want to ensure that A->child->child will always be NULL and no one can change this. ( Ensuring a link list to have no more than two nodes ).
Any help ?
It can’t be done, not using a single link anyways. If you have a link pointing back to the “parent” it might be done by having the
childandparentmember variables private, and then have special access functions that makes sure you can not add more than two links to the first object in the chain.The reason I suggest using a “parent” chain is because you could force the first object in the chain to check it’s
childchain, but if you have access to e.g. the last object in the chain you could add two more children in that chain.