28 October 2018

C++: Template of retrieving array size

 template<typename T, std::size_t S>  
 std::size_t SizeOf(T(&)[S]) {  
      return S;  
 }  
 int main()  
 {  
      double d[10];  
      cout << "d size is " << SizeOf(d) << endl;  
      char ch;  
      cin.getline(&ch, 1);  
     return 0;  
 }  

C++: Using shared pointer to point to object array in object tree example

 #include "stdafx.h"  

 #include <iostream> 
 using namespace std; 
 static int numobject = 0; 
 #define NUM_CHILDREN 3 
 template<typename T> 
 inline std::shared_ptr<T> MakeArray(int size) 
 { 
      return std::shared_ptr<T>(new T[size], [](T *p) { delete[] p; }); 
 } 
 template<typename T> 
 inline std::shared_ptr<T> MakeArray2(int size) 
 { 
      return std::shared_ptr<T>(new T[size], std::default_delete<OtC[]>()); 
 } 
 class OtC 
 { 
 public: 
      OtC() 
      { 
           id = ++numobject; 
           cout << "Otc constructor " << id << endl; 
      } 
      ~OtC() 
      { 
           cout << "Otc destructor " << id << endl; 
      } 
      int id; 
      std::shared_ptr<OtC> children = nullptr; 
      OtC * parent; 
      std::shared_ptr<OtC> GetChildren() 
      { 
           if (children == nullptr) 
           { 
                cout << "creaing children for the first time" << endl; 
                CreateChildren(); 
                return children; 
           } 
           else 
           { 
                cout << "children has already been created" << endl; 
                return children; 
           } 
      } 
      void CreateChildren() 
      { 
           OtC* parent = this; 
           cout << "In create children" << endl; 
           children = MakeArray<OtC>(NUM_CHILDREN); 
           children.get()[0].id = children.get()[0].id + 10; 
           children.get()[1].id = children.get()[1].id + 10; 
           children.get()[2].id = children.get()[2].id + 10; 
           children.get()[0].parent = this; 
           children.get()[1].parent = this; 
           children.get()[2].parent = this; 
      } 
 }; 
 void SingleObject() 
 { 
      shared_ptr<OtC> o = make_shared<OtC>(); 
 } 
 void MultiObjects() 
 { 
      std::shared_ptr<OtC> p = MakeArray2<OtC>(4); 
 } 
 void ChainObject() 
 { 
      shared_ptr<OtC> o = make_shared<OtC>(); 
      shared_ptr<OtC> children = o->GetChildren(); 
      cout << "first call use count " << children.use_count() << endl; 
      for (int i = 0; i < NUM_CHILDREN; i++) 
      { 
           cout << "child " << i << " id is " << children.get()[i].id << endl; 
      } 
      shared_ptr<OtC> children2 = o->GetChildren(); 
      cout << "second call use count " << children.use_count() << endl; 
      for (int i = 0; i < NUM_CHILDREN; i++) 
      { 
           cout << "child " << i << " id is " << children.get()[i].id << endl; 
           cout << "child " << i << " parent id is " << children.get()[i].parent->id << endl; 
      } 
 } 
 int main() 
 { 
      cout << "Shared pointer object children tree example" << endl; 
      ChainObject(); 
      char ch; 
      cin.getline(&ch, 1); 
   return 0; 
 }