summaryrefslogtreecommitdiff
path: root/src/tests/sptr.cpp
blob: bc00f787078d1b4046a910b0e6b2884fb6a5d5a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include "sptr.h"

class Annoy
{
public:
	Annoy() : nCnt( 0 )
	{
		printf("Created.\n");
	}

	virtual ~Annoy()
	{
		printf("Destroyed.\n");
	}

	virtual void go()
	{
		printf("%d: I'm annoying.\n", ++nCnt);
	}

	int nCnt;
};

class Annoy2: public Annoy
{
public:
	Annoy2(){};
	virtual ~Annoy2(){};
	virtual void go()
	{
		printf("{{I'm Annoy2!!}} ");
		Annoy::go();
	}
	virtual void go2()
	{
		printf("This is me, on my own...\n");
	}
};

void beAnnoying( SPtr<Annoy> bob )
{
	printf("bob-Count: %d\n", bob.count() );
	bob->go();
}

int main()
{
	SPtr<Annoy> pt( new Annoy2 );
	printf("Count: %d\n", pt.count() );
	pt->go();

	{
		SPtr<Annoy> pt2 = pt;
		printf("Count: %d\n", pt2.count() );

		pt2->go();

		{
			SPtr<Annoy> pt3( pt2 );
			printf("Count: %d\n", pt3.count() );

			pt3->go();

			beAnnoying( pt3 );

			{
				SPtr<Annoy2> pt4( dynamic_cast<SPtr<Annoy2> >(pt3) );
				printf("Count: %d\n", pt4.count() );

				pt4.go2();
			}
		}
		printf("Count: %d\n", pt.count() );
	}
	printf("Count: %d\n", pt.count() );
}