Im playing with my smart pointer class and I want to implement ++ and — operators with the following behavior: If pointer points to single variable or if it points to array and ++(–) moves pointer out of array bounds an exception should be thrown when trying to ++(–).
Something like that:
class A;
SmartPtr<A> s(new A[3]);
SmartPtr<A> s1(new A());
++s;//ok
--s;//ok
--s;//exception OutOfBounds thrown
++s1;//exception OutOfBounds thrown
--s1;//exception OutOfBounds thrown
I tried to use typeid. But it returns A type anyway.
A* arr=new A[3];
typeid(arr).name();//type is P1A
typeid(--arr).name();//type is P1A
typeid(arr+7).name();//type is P1A
So is there any way to determine does pointer point to “my” type of object after ++(–)?
new A[3]returns anA*, just likenew Adoes. So you can’t distinguish them.If you want your class to do bounds checking, then you will need to explicitly tell it how many items are in the array.