#include "ns3/object.h" using namespace ns3; class AnObject : public Object { public: static const InterfaceId iid; AnObject (); protected: virtual void DoDispose (void); }; const InterfaceId AnObject::iid = MakeInterfaceId ("AnObject", Object::iid); AnObject::AnObject () { // enable our interface SetInterfaceId (AnObject::iid); } void AnObject::DoDispose (void) { // Do your work here. // chain up Object::DoDispose (); } class AnotherObject : public Object { public: static const InterfaceId iid; AnotherObject (int a); private: virtual void DoDispose (void); }; const InterfaceId AnotherObject::iid = MakeInterfaceId ("AnotherObject", Object::iid); AnotherObject::AnotherObject (int a) { // enable our interface SetInterfaceId (AnotherObject::iid); } void AnotherObject::DoDispose (void) { // Do your work here. // chain up Object::DoDispose (); } class YetAnotherObject : public Object { public: static const InterfaceId iid; YetAnotherObject (int a); private: virtual void DoDispose (void); }; const InterfaceId YetAnotherObject::iid = MakeInterfaceId ("YetAnotherObject", Object::iid); YetAnotherObject::YetAnotherObject (int a) { // enable our interface SetInterfaceId (YetAnotherObject::iid); // aggregated directly to another object. AddInterface (Create ()); } void YetAnotherObject::DoDispose (void) { // Do your work here. // chain up Object::DoDispose (); } int main (int argc, char *argv[]) { Ptr p; Ptr anObject; Ptr anotherObject; Ptr yetAnotherObject; p = Create (); // p gives you access to AnObject's interface anObject = p->QueryInterface (AnObject::iid); NS_ASSERT (anObject != 0); // p does not give you access to AnotherObject's interface anotherObject = p->QueryInterface (AnotherObject::iid); NS_ASSERT (anotherObject == 0); anotherObject = Create (1); // AnotherObject does not give you access to AnObject's interface anObject = anotherObject->QueryInterface (AnObject::iid); NS_ASSERT (anObject == 0); // aggregate the two objects p->AddInterface (anotherObject); // p gives you access to AnObject's interface anObject = p->QueryInterface (AnObject::iid); NS_ASSERT (anObject != 0); // p gives you access to AnotherObject's interface anotherObject = p->QueryInterface (AnotherObject::iid); NS_ASSERT (anotherObject != 0); yetAnotherObject = Create (2); // gives you acess to AnObject interface too. anObject = yetAnotherObject->QueryInterface (AnObject::iid); NS_ASSERT (anObject != 0); return 0; }