Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it allowed in C++ to modify a constant object's pointer member variable's memory from outside?

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!

like image 502
Kristof01 Avatar asked Aug 05 '15 12:08

Kristof01


People also ask

Can you modify the constant variable in C?

The constant variable values cannot be changed after its initialization. In this section we will see how to change the value of some constant variables. If we want to change the value of constant variable, it will generate compile time error. Please check the following code to get the better idea.

Can we change the value of constant pointer?

We can change the value stored in the pointer and make it point to another constant variable.

What is the purpose of declaring a constant pointer?

A'constant pointer' is a pointer that cannot change the address it is containing. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

How do you change a constant value in CPP?

Changing Value of a const variable through pointerBy assigning the address of the variable to a non-constant pointer, We are casting a constant variable to a non-constant pointer. The compiler will give warning while typecasting and will discard the const qualifier.


2 Answers

"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."

You are quite right about this, however the pointer stored inside the X-object points outside the object. The outside isn't affected by X's constness, just the data stored inside X.

like image 131
Bo Persson Avatar answered Sep 22 '22 01:09

Bo Persson


Why do you think that, because the pointer is constant, that which is pointed to should be constant? It does not necessarily follow.

Sometimes you need a pointer that must always point to a particular place, but through which you can modify the indicated place. There's nothing about class X which suggests that you shouldn't be able to change the memory data points to.

More importantly, you're thinking wrongly about the const keyword. Given

X& operator=(const X& other)

all you are doing is telling the compiler that you do not intend to change other inside operator=() and asking the compiler to prevent you from doing so in case you forget. Nothing more, nothing less. It says nothing at all about the const-ness of other outside of operator=() nor of the const-ness of anything any pointer inside other points to.

like image 32
Rob K Avatar answered Sep 20 '22 01:09

Rob K