From e19acafd485a1c6a027bc967945d914fa5ed38b9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 24 Nov 2006 10:40:34 +0000 Subject: Added a simple smart-pointer. It deletes what's in it automatically and does ref-counting. You just have to be careful to not hand it an array... --- src/tests/sptr.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/tests/sptr.cpp (limited to 'src/tests') diff --git a/src/tests/sptr.cpp b/src/tests/sptr.cpp new file mode 100644 index 0000000..38d3675 --- /dev/null +++ b/src/tests/sptr.cpp @@ -0,0 +1,55 @@ +#include +#include "sptr.h" + +class Annoy +{ +public: + Annoy() : nCnt( 0 ) + { + printf("Created.\n"); + } + + ~Annoy() + { + printf("Destroyed.\n"); + } + + void go() + { + printf("%d: I'm annoying.\n", ++nCnt); + } + + int nCnt; +}; + +void beAnnoying( SPtr bob ) +{ + printf("bob-Count: %d\n", bob.count() ); + bob->go(); +} + +int main() +{ + SPtr pt( new Annoy ); + printf("Count: %d\n", pt.count() ); + pt->go(); + + { + SPtr pt2 = pt; + printf("Count: %d\n", pt2.count() ); + + pt2->go(); + + { + SPtr pt3( pt2 ); + printf("Count: %d\n", pt3.count() ); + + pt3->go(); + + beAnnoying( pt3 ); + } + printf("Count: %d\n", pt.count() ); + } + printf("Count: %d\n", pt.count() ); +} + -- cgit v1.2.3