How can I detect if a pointer is const?

I have the following, very simple class that allows automatic destruction of an array:

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
//Smart array pointer.  The last array element must be zero, or
//it must evaluate to zero when cast to bool.
template<class T, class CopyType>
class SmartArrayPtr
{
	//Control variable to avoid copies to involuntarily release
	//the array.
	bool _isCopy;
	void ReleaseArray(void)
	{
		if (ptr && !_isCopy)
		{
			//int i = 0;
			//while (ptr[i])
			//{
			//	CopyType::destroy(&ptr[i]);
			//	i++;
			//}
			delete[] ptr;
			ptr = NULL;
		}
	}

public:
	T *ptr;
	SmartArrayPtr<T, CopyType>() : ptr(NULL), _isCopy(false)
	{ }
	//Initialization constructors
	SmartArrayPtr<T, CopyType>(T *thePtr) : ptr(thePtr), _isCopy(false)
	{ }
	//Copy constructor
	SmartArrayPtr<T, CopyType>(const SmartArrayPtr<T, CopyType>& src) : ptr(src.ptr), _isCopy(true)
	{ }
	~SmartArrayPtr()
	{
		ReleaseArray();
	}
	T* Detach()
	{
		T *oldPtr = ptr;
		ptr = NULL;
		return oldPtr;
	}
	T& operator[](int index)
	{
		return ptr[index];
	}
	T& operator=(T *newPtr)
	{
		ReleaseArray();
		ptr = newPtr;
		return ptr;
	}
	T& operator&()
	{
		return &ptr;
	}
};


Whenever the object is destroyed, the ReleaseArray() routine jumps in and deletes the array. But what if T = const SomeDataType? How can I re-write ReleaseArray() so it accounts for constness?

I hope it is clear, but let me know if not. Thanks!
For like cases there is a key word mutable
so please see new version:
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
//Smart array pointer.  The last array element must be zero, or
//it must evaluate to zero when cast to bool.
template<class T, class CopyType>
class SmartArrayPtr
{
	//Control variable to avoid copies to involuntarily release
	//the array.
	bool _isCopy;
	void ReleaseArray(void) const
	{
		if (ptr && !_isCopy)
		{
			//int i = 0;
			//while (ptr[i])
			//{
			//	CopyType::destroy(&ptr[i]);
			//	i++;
			//}
			delete[] ptr;
			ptr = NULL;
		}
	}

public:
	mutable T *ptr;
	SmartArrayPtr<T, CopyType>() : ptr(NULL), _isCopy(false)
	{ }
	//Initialization constructors
	SmartArrayPtr<T, CopyType>(T *thePtr) : ptr(thePtr), _isCopy(false)
	{ }
	//Copy constructor
	SmartArrayPtr<T, CopyType>(const SmartArrayPtr<T, CopyType>& src) : ptr(src.ptr), _isCopy(true)
	{ }
	~SmartArrayPtr()
	{
		ReleaseArray();
	}
	const T* Detach() const
	{
		T *oldPtr = ptr;
		ptr = NULL;
		return oldPtr;
	}
	T* Detach()
	{
		T *oldPtr = ptr;
		ptr = NULL;
		return oldPtr;
	}
        const T& operator[](int index) const
	{
		return ptr[index];
	}
	T& operator[](int index)
	{
		return ptr[index];
	}
	T& operator=(T *newPtr)
	{
		ReleaseArray();
		ptr = newPtr;
		return ptr;
	}
	T& operator&()
	{
		return &ptr;
	}
};
Hi Denis. Thanks for dropping by.

I have a couple of doubts about your proposition.

First of all, I see that you added the const keyword twice in certain overloads. Why is that? Shouldn't just one of those suffice?

Second: I have read a bit about the mutable keyword. In short, it seems that mutable allows the contents of a variable to change from const members. I don't think I want that. I want a way to avoid the call to delete[] in ReleaseArray(). The array received is const because it is allocated in the stack. Example:

1
2
3
4
5
6
7
8
9
const int* GetArray(void)
{
    static const int myArray[] = {
        1,
        2,
        3,
        0 };
    return &myArray[0];
}


So as you can see, the array is not dynamically allocated, and therefore it must not be deleted.
Last edited on
There's no way to distinguish dynamic from non-dynamic arrays with just a pointer. The user will have to somehow tell the class how the array was created.
I see. Understood. So maybe an additional parameter in the constructor.

Thanks!
Topic archived. No new replies allowed.