How can I create a Modifiable Const vector but make it unable to be resized? Either that OR I want to return a pointer from a function but not allow the user to delete it.
This is my example so far:
class MemberRawArrayVector
{
private:
vector<unsigned char> MemberRawArray;
public:
MemberRawArrayVector(int Size) : MemberRawArray(Size) {}
void SetMemberValue(char Value, int Index) { MemberRawArray[Index] = Value; }
const vector<unsigned char>* GetMemberArray() { return &MemberRawArray; }
};
class RawMemberArray
{
private:
unsigned char* RawArray;
RawMemberArray(int Size) : RawArray(new unsigned char[Size]) {}
~RawMemberArray() { delete[] RawArray; }
void SetMemberValue(char Value, int Index) { RawArray[Index] = Value; }
unsigned char* GetMemberArray() { return RawArray; }
};
In the first class with vectors, it returns the member array and it is read only to prevent resizing. Thing is, I want to be able to modify the values in it without the SetMemberValue function.
In the second class, I return the raw pointer but as we know, the user can easily delete this pointer before the destructor gets to it. I want to prevent deletion by force! Inother words I want them to have access to the pointer but at the same time not allow them to delete it at all. Only the class should be able to do deletion. I cannot have a const pointer because I need to modify the values it points to.
This is the reason I created the two classes as examples. One has what the other doesn't. The vector can't be delete/resized but it also can't be modified. The raw pointer can be modified and deleted!
Is this impossible? I don't want to use SetMemberValue function.. I'd prefer the pointer over the vector if it has such a solution or maybe both so I can choose but I'm a bit skeptical on whether this is possible or not.
Note: I also tried Smart Pointers with a custom deleter but then realize SmartPointers can also be deleted.