+ 1
How to avoid const member function updating value of pointer variable
Hello As we know, const member function ensure that value of members are not updated within function. As we may have pointer, const helps to prevent pointer being pointed to other variable. but it does not prevent value being modified. How to achieve this? In conclusion, Refer attached code and help me prevent *p = 2 as allowed compilation from function void Test::display() const Thanks a lot for your help in advance...! https://code.sololearn.com/cwsJlWRRujb9
9 odpowiedzi
+ 2
Try to declare <p> like this
const int* p {nullptr};
And considering <p> is an uninitialized pointer (points to what?), it is wrong to change its underlying value.
IIRC that is not what const member function was meant for. I'll take a look in the lesson to refresh my memory.
In the meantime, check this one by our friend ~ swim ~
https://www.sololearn.com/post/86189/?ref=app
+ 1
AFIK, const member function are there to avoid non intentional change in object i.e. data members.
https://www.tutorialspoint.com/const-member-functions-in-cplusplus#:~:text=The%20const%20member%20functions%20are,by%20any%20type%20of%20object.
That's why we generally have all getter methods as const function... We just make method const rather than member variable as const.... This means our member variable may or may not change but const method suggest that this method is not intended to change member variables...
Yes , in my query question; one const is applied to pointer variable by const method and it does prevent updating pointer pointing to something else from that method....however , const applied to method doesn't prevent modifying value of location it is pointing.
+ 1
Yes that's right bro, const member functions and const instance is working together to prevent unintended modifications to the members of the class.
I just checked the lesson again, in the lesson it says the instance that owns the const member function is defined also as const.
https://www.sololearn.com/learn/CPlusPlus/1895/
+ 1
I am sorry but I could not get your answer... Could you please reiterate the same ? I am not talking about const instance i.e. in my case , obj is not const....
I cannot make obj const as in sone other function which is non const is changing value of other member variable...
To make it clear , is it what you suggest that current code is limitation of c++? In other word, const member function doesn't ensure constantness of pointer and it's value both ? For this purpose, we have to use non const obj or instance...
+ 1
However, I just made object const
(const Test obj;) in my question code and it still compiles i.e. *p is getting modified still...
+ 1
Ketan Lalcheta
Looks like your problem needs assistance from a more C++ knowledgeable person. I don't dare to say/suggest what you found is a "limitation" of C++.
It could be because <p> was a non const pointer, or something I really have no knowledge about. I was just saying what I understood from the lesson, where it says an initialized object made const is supposedly becoming a read-only object (non modifiable).
0
Why initialize value of pointer <p> in `display()` rather than in a constructor?
0
Just want to check that it should not be allowed to modify in const method.....
0
No worries Ipang ... We just learn from this way specially I would say for myself... I really appreciate your effort and help.... Many thanks for your support...
Also for your reference and all others from community, I have updated code ...
Added Test_New class.... It has const pointer as member and initialised in constructor initialisation list... Still *p = 2 is valid in display as const method for Test_new class..