diff --git a/datatype/LifetimeTest.cpp b/datatype/LifetimeTest.cpp index 3aab504..d761f69 100644 --- a/datatype/LifetimeTest.cpp +++ b/datatype/LifetimeTest.cpp @@ -35,7 +35,7 @@ public: public: - TestArray(RawLifetimeCallback *cb) + TestArray(TCB *cb) { life_cb=cb; @@ -96,21 +96,58 @@ template class TestObject public: - TestObject(){value=0;} + TestObject() + { + value=0; + std::cout<<"TestObject()"< class TestObjectArray:public Test> +template class TestObjectArray:public TestArray> { - DefaultObjectLifetimeCallback life_cb;; + DefaultObjectLifetimeCallback life_cb; public: - TestObjectArray():TestArray>(&life_cb){} - ~TestObjectArray()=default; + TestObjectArray():TestArray>(&life_cb){} + virtual ~TestObjectArray() + { + for(T *p:*this) + life_cb.Clear(&p); + } + + T *Create() + { + T *p; + + if(!life_cb.Create(&p)) + return nullptr; + + TestArray>::Add(p); + + return p; + } }; void main() @@ -170,14 +207,22 @@ void main() { TestObjectArray> ta_obj; - ta_obj.Add(); - ta_obj.Add(); - ta_obj.Add(); + ta_obj.Add(new TestObject(1)); + ta_obj.Add(new TestObject(2)); + ta_obj.Add(new TestObject(3)); + + TestObject *p=ta_obj.Create(); + + p->Set(4); + + TestObject *two[2]={new TestObject(9),new TestObject(0)}; + + ta_obj.Add(two,2); std::cout<<"TestArray>: "; - for(const TestObject &obj:ta_obj) - std::cout<<"{"< *obj:ta_obj) + std::cout<<"{"<Get()<<"} "; std::cout< +#include #include using namespace hgl;