smartPointer ?

Hi,

I read quite a lot of topics about them, but I can not run The following code.
Thanks for reading!


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
template<class T>
class smartPTR{
	T *mp;
	//T *Array;
	//int size;
	unsigned *ref;
	void DecrementRef()
	{
		if(ref)
		{
			--(*ref);  // dec the ref
			if(!*ref)  // if it's zero
			{
        // no more Smart Pointers using this data, so clean up
			 delete mp;
			 delete ref;
			}
		}
	}


public:
	smartPTR():mp(0),ref(0){}
	smartPTR(const smartPTR<T> &r):mp(r.mp), ref(r.ref)
	{
		if(ref)
		  ++(*ref);

	} 

	explicit smartPTR(T *p):mp(p), ref(new unsigned(1)){}
	/*smartPTR(T *p ,T *v ,int s)
	{ 
		mp = &p[0];
		Array = v;
		size = s;
	}*/

	smartPTR<T> &operator=(const smartPTR<T> &r)
	{
		DecrementRef();

		ptr = r.ptr;
		ref = r.ref;

		if(ref)
		  ++(*ref);

		return *this;
	}

	~smartPTR()
	{
		DecrementRef();
	}
	T &operator*()
	{
		return *mp;
	}
	T *operator->()
	{
		return mp;
	}

	T &operator[](int i)
	{
		return mp[i];
	}

	smartPTR &operator++()
	{
		this->mp++;
		return *this;
	}
	smartPTR operator++(int);
	smartPTR &operator--();
	smartPTR operator--(int);

};


class firix{
	int x;
public:
	firix(int xx = 0):x(xx){}
	void display()const
	{
		cout << "x :" <<  x  << endl;

	}


};


int main()
{
	firix f(10);
	firix *fp = &f;
	smartPTR<firix> ptr(fp);		




	
	return 0;
}
Last edited on
on line 98 - 100 you delete an object that's not created with new and that will crash.
what I need to do?
Obviously your smart pointer needs to be initialized with a pointer to heap allocated memory. If you don't understand that then I don't understand why you are writing your own smart pointer in the first place.
I want to learn how to do.
firix wrote:
what I need to do?
You must not use pointers to the stack.

smartPTR<firix> ptr(new firix(10)); is the only way to initialize the pointer
@coder777


I did that already.

@Zhuge
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/dynamic/

I have read these issues already.

I want to thank those who have taken the time to respond to my question.

smartPTR operator++(int);
smartPTR &operator--();
smartPTR operator--(int);

How can I write code for these functions ?
Your code looks OK to me but has a few problems:

1) You need to protect against self assignment. Think about what will happen if someone does this:

1
2
3
smartPTR<int> foo = new int(10);

foo = foo;  // self assignment -- will make your class explode 


2) You are not const correct

3) Since your smart pointer class is only for single element pointers (since you're using delete and not delete[]), you should not have the [] operator.

4) You also should not have -- or ++ operators, because changing the pointer will make it impossible to delete properly. And again is pointless for a single element pointer class.

5) As coder777 said, smart pointer classes are meant to manage memory allocated with new. If you are not allocating the object with new, there's no reason to use a pointer class (and the pointer class will crash.

6) up/downcasting for polymorphism would be a plus
thanks Disch.
Topic archived. No new replies allowed.