#include #include #include using namespace Cntm; using namespace std; class TNo { public: typedef RefPtr Ptr; int g; }; class TCP1: virtual public RefBase { public: typedef RefPtr Ptr; int g; }; class TB1 { public: int b1; virtual void V1() { int a = b1; } }; class TB2: virtual public IRefObject { public: typedef RefPtr Ptr; int b2; virtual void V2() { int a = b2; } virtual void V4() { int a = b2; } }; class TB3: virtual public IRefObject { public: typedef RefPtr Ptr; int b3; virtual void V3() { int a = b3; } virtual void V4() { int a = b3; } }; class TD1: TNo, virtual public TB1, virtual public TB2, virtual public TB3, virtual public RefBase { protected: ~TD1() { cerr << ""; delete agg1; } public: typedef RefPtr Ptr; class TAgg1: public AggregatedBase { public: typedef RefPtr Ptr; TAgg1(TD1* agg): AggregatedBase(agg) {} int t; }; TAgg1* agg1; TAgg1::Ptr Agg1() { return *agg1; } int b4; TD1(): TB1(), TB2(), TB3(), RefBase() { agg1 = new TAgg1(this); } void V1() { int a = b4; } void V2() { int a = b4; } void V3() { int a = b4; } void V4() { int a = b2; } //void IncRefCount() //{ RefBase::IncRefCount(); } //void DecRefCount() //{ RefBase::DecRefCount(); } }; void RefPtrExp1() { TCP1* RO1 = new TCP1; TCP1::Ptr P = *RO1; P->g = 9; Cntm::IRefObject::Ptr P1 = P; P1 = *RO1; IRefObject::Ptr P2 = *RO1;//P; TD1* d1 = new TD1; TB2::Ptr pb2(d1); // TD1* d2 = new TD1; cerr << "gggggg"; IRefObject::Ptr pb6 = *d1; cerr << "jjjjjj"; pb2->V2(); // cerr << "gggggg"; TD1::Ptr pd1 = *d1; cerr << "jjjjjj"; pd1->V4(); TB3::Ptr pb3 = pd1; pb3->V3(); pb3->V4(); pd1.SetNull(); pd1.SetPointer(dynamic_cast(pb2.Pointer())); pd1 = TD1::Ptr::DynamicCast(pb2); pd1->V1(); pd1->V2(); pd1->agg1->t = 7; TD1::TAgg1::Ptr pa1 = pd1->Agg1(); pa1->t += 9; std::cout << d1->agg1->t << std::endl; TB3* tpb3 = pd1.Pointer(); tpb3->V3(); TNo::Ptr np1(new TNo); np1.SetNull(); } void RefPtrGenericTest() { TD1::Ptr pd1(new TD1); GenericNoRefPtr gnp1 = pd1->Agg1(); GenericRefPtr gp1 = pd1; IRefObject* p1 = gp1.DynamicCast(); RefBase* p2 = static_cast(pd1.Pointer()); IRefObject* p3 = static_cast(p2); TCP1::Ptr pb2(new TCP1); GenericRefPtr gp2 = pb2; IRefObject* obj1 = gp2.DynamicCast(); } class GMBase { public: bool flag; GMBase(): flag(false) {} virtual void M1(int i) { cerr << "GMBase: i = " << i << endl; flag = true; } void Check() { if (!flag) throw Exception("GMBase", "Не работают указатели на методы"); } }; class GM1: public GMBase, public RefBase { public: void M1(int i) { cerr << "GM1: i = " << i << endl; flag = true; } }; void GenericMethodPtrTest() { GMBase b1; GM1 c1; typedef GenericMethodRefPtr TPM; typedef GenericMethodNoRefPtr TPM2; TPM p1(&b1, &GMBase::M1); (p1.Object()->*p1.Method())(2); b1.Check(); // Вызов виртуального метода через обобщенные указатели TPM p2(&c1, &GMBase::M1); (p2.Object()->*p2.Method())(3); c1.Check(); TPM2 p3; p3 = p1; TPM2 p4 = p2; p1 = p4; } void RefPtrExp() { RefPtrExp1(); RefPtrGenericTest(); GenericMethodPtrTest(); RefBaseEx::WaitDestroyAll(20000); }