Is it possible to wrap a raw C++ pointer in a smart pointer-like class, which will allow a user to update using familiar operators such as array and indirection:
int i;
my_ptr<int> ptr(i);
ptr[i] = 42;
yet, will absolutely deny that user access to the underlying raw addresses. So, this should not succeed:
int *p;
p = &ptr[i];
Alas, I fear I may be asking the impossible. I could use getter and setter methods, but I’m curious if I can do without.
Filter access through a proxy class, such as this(incomplete example):
That class needs some more work, but once complete, if your
operator[]returns one of those, it can be assigned to from aT, but you can’t get the address of theT.Edit
Thanks for the votes guys. But this answer isn’t quite as good as you think. It won’t allow the user to do any other operations than those defined in the proxy. So, for example, this wouldn’t work:
Unless the proxy class was specialized for each type, and who wants to do that?