0
class SomeClass{

private:
    int* p;

public:
    SomeClass() {p = new int();}

    void foo() const {*p = 20;}

};

//...

const SomeClass obj;
obj.foo();

Let's say I consider the pointed-to object as part of the constness of the class. Do I have any way to make sure it's not modified in const functions (apart from not forgetting about it)? (No, I don't mean making it const int*)

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
AdyAdy
  • 988
  • 6
  • 19
  • 2
    https://en.cppreference.com/w/cpp/experimental/propagate_const – imreal Aug 03 '18 at 18:36
  • highly related: https://stackoverflow.com/questions/6852717/what-can-a-const-method-change. I'm trying to see if there is a duplicate. – NathanOliver Aug 03 '18 at 18:37
  • TL;DR of the dupe: you an object that will work properly in a const function. such as `string` for `char*` and `vector` for most other types. – NathanOliver Aug 03 '18 at 18:48
  • The "duplicate" has no answer to this question at the moment. They ask about array-pointers and all answers suggest std::vector. It was 6+ years ago, I assume propagate_const didn't exist at that time. – AdyAdy Aug 03 '18 at 20:23

0 Answers0