   Self referencing in C# vs C++
  Hi,I am currently trying to find a good way to implement a quad tree for terrain rendering. incidentally, I realised some important differences between C# and C++ but I need you guys to help confirm my observation, otherwise, advise alternative implementation.in C#, I can doclass QuadTree{  QuadTree[] child = new QuadTree[4];}but in C++, in order to handle the memory usage transparent to the user, I have to doclass QuadTreeNode{  QuadTreeNode* child[4];  ~QuadTreeNode() { /*foreach child i*/ delete child; }}class QuadTree{  QuadTreeNode* root;  ~QuadTreeNode()  {    delete root;  }}The C# version is definitely more intuitive, while the C++ version is quite cumbersome. Anyone can comment on whether the C++ version is a good implmentation?Thanks =)   
  huh? you can do the same in c++...class QuadTree{  QuadTree(void)  {    child[0] = new QuadTree;    child[1] = new QuadTree;    child[2] = new QuadTree;    child[3] = new QuadTree;  }  ~QuadTree(void)  {    delete child[0];    delete child[1];    delete child[2];    delete child[3];  }  QuadTree* child[4];}EDIT: actually that is just plain stupid. it creates a unlimeted ammound of quadtress....   ;Quote:Original post by jakeseeAnyone can comment on whether the C++ version is a good implmentation?You should fill the array with null pointers in the constructor. Otherwise you invoke undefined behavior when the destructor tries do delete the pointees.;  You could use standard C++.  The default constructors and destructors take care of everything for you.  Use C++ properly and the magic just works.#include <tr1/array>#include <tr1/memory>class QuadTree{public:typedef std::tr1::shared_ptr<QuadTree> pointer;private:std::tr1::array<pointer, 4> child;};int main(){QuadTree q;}Really, the standard library is there for you.  It's a standard part of the language.  Standard.   ;  I don't think Trees should share their nodes...   ;  They shouldn't, but you don't have to use the shared-ness of shared pointers. It's just a smart pointer type. auto_ptr would probably be at least as bad here. What you really want is something like mr_edd's value_ptr, but I don't think anything like that has made it into C++0x, let alone TR1.   ;Quote:Original post by jakeseeI am currently trying to find a good way to implement a quad tree for terrain rendering.Does the tree change?Will existing tree need to be updated?   ;Quote:Original post by ZahlmanI don't think anything like that has made it into C++0xWhat about unique_ptr?   ;Quote:Original post by DevFredQuote:Original post by ZahlmanI don't think anything like that has made it into C++0xWhat about unique_ptr?That does still not 100% fit, it being movable.   ;Quote:Original post by jakeseeThe C# version is definitely more intuitive, while the C++ version is quite cumbersome. Anyone can comment on whether the C++ version is a good implmentation?Your C++ version is not a good implementation. It's bad to manage memory by yourself (hence all the replies about smart-pointer types), plus your C++ code is buggy - it doesn't follow the rule of 3.Easiest way to convert your C# example to C++, to me, is:class QuadTree{  QuadTree() : child(4) {}//'child' is initialised to hold 4 new QuadTrees  std::vector<QuadTree> child;//member array called 'child'}Though both the C# and C++ versions seem to recurse infinitely...[Edited by - Hodgman on May 12, 2010 3:30:46 AM]