I've been trying to understand when I write a function in C++ with a constant argument and a pointer variable inside of that object than the const flag is not protecting the underlying memory against modifications.
For example it's perfectly legal to do the following in the operator=()
function of the class called X
:
class X
{
public:
X& operator=(const X& other)
{
this->data = other.data; //(*)
return *this;
}
private:
int* data;
};
(*): This is the same as the following:
int* some_pointer;
int* const other_pointer = some_pointer;
int* class_pointer = other_pointer;
But not the same as:
const int* other_pointer;
int* class_pointer = other_pointer;
Which would generate the following error:
error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
int* class_pointer = other_pointer;
^
I understand why other.x
is being casted to a int* const
but I don't see why it isn't being casted to a const* int
at the same time (which is a const int* const
). When I write a function with a const
argument my logic suggests anything inside that argument should inherit the constness because that should be the purpose of const
, to protect the underlying data against modification.
When a pointer member is being accessed from outside of the const
version of a class I think it should be a reasonable expectation that the object's const
keyword should protect anything (even the memory) that gets out of the class from modification. The argument against this is that the outside memory does not belong to the object so it shouldn't be it's responsiblity to protect it either. My perspective on it is that in this case (out of any other cases when it's being accessed somewhere else with any kind of access rights) we are taking something out of a const
object. In other words it's giving visibility to something outside of itself. So what's the reason behind not making the visibility const
? That wouldn't change the accessibility rights of the memory in any other location of the code!
const
does in the D programming language. – Musclebound