Skip to main content
deleted 1418 characters in body
Source Link
vsoftco
  • 56.9k
  • 12
  • 150
  • 269

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contriveduse an abomination like this, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bitconst_cast-wise const). Or, even much more contrived, via aing const_castthis in the modifying method (this actually fits exactly your question). Example of the first (via mutable):

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

And of the second (via pointer):

#include <iostream> struct Foo { int x = 0; void f(Foo* foo) const { ++foo->x; } void modify_x(Foo* foo) const { f(foo); // call const member function here that actually changes the instance // ++x; // this won't work } }; int main() { Foo foo; foo.modify_x(&foo); std::cout << foo.x << std::endl; } 

And the last (that actually directly addresses your question) - an abomination like this, using const_castconst calling function (but you have to make sure you invoke your function on non-const objects, otherwise it leads to undefined behaviour):

#include <iostream> struct Foo { int x = 0; void f() { ++x; } void modify_x() const { const_cast<Foo*>(this)->f(); // call non-const member function } };   int main() { Foo foo; std::cout << foo.x << std::endl; foo.modify_x(); std::cout << foo.x << std::endl; } 

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contrived, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bit-wise const). Or, even much more contrived, via a const_cast in the modifying method (this actually fits exactly your question). Example of the first (via mutable):

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

And of the second (via pointer):

#include <iostream> struct Foo { int x = 0; void f(Foo* foo) const { ++foo->x; } void modify_x(Foo* foo) const { f(foo); // call const member function here that actually changes the instance // ++x; // this won't work } }; int main() { Foo foo; foo.modify_x(&foo); std::cout << foo.x << std::endl; } 

And the last (that actually directly addresses your question) - an abomination like this, using const_cast (but you have to make sure you invoke your function on non-const objects, otherwise it leads to undefined behaviour:

#include <iostream> struct Foo { int x = 0; void f() { ++x; } void modify_x() const { const_cast<Foo*>(this)->f(); // call non-const member function } };   int main() { Foo foo; std::cout << foo.x << std::endl; foo.modify_x(); std::cout << foo.x << std::endl; } 

The answer is NO. The long answer is perhaps YES, provided you use an abomination like this, via const_cast-ing this in the const calling function (but you have to make sure you invoke your function on non-const objects, otherwise it leads to undefined behaviour):

#include <iostream> struct Foo { int x = 0; void f() { ++x; } void modify_x() const { const_cast<Foo*>(this)->f(); // call non-const member function } }; int main() { Foo foo; std::cout << foo.x << std::endl; foo.modify_x(); std::cout << foo.x << std::endl; } 
added 605 characters in body
Source Link
vsoftco
  • 56.9k
  • 12
  • 150
  • 269

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contrived, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bit-wise const). Or, even much more contrived, via a const_cast in the modifying method (this actually fits exactly your question). Example of the formerfirst (via mutable):

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

And of the lattersecond (via pointer):

#include <iostream> struct Foo { int x = 0; void f(Foo* foo) const { ++foo->x; } void modify_x(Foo* foo) const { f(foo); // call const member function here that actually changes the instance // ++x; // this won't work } }; int main() { Foo foo; foo.modify_x(&foo); std::cout << foo.x << std::endl; } 

And the last (that actually directly addresses your question) - an abomination like this, using const_cast (but you have to make sure you invoke your function on non-const objects, otherwise it leads to undefined behaviour:

#include <iostream> struct Foo { int x = 0; void f() { ++x; } void modify_x() const { const_cast<Foo*>(this)->f(); // call non-const member function } }; int main() { Foo foo; std::cout << foo.x << std::endl; foo.modify_x(); std::cout << foo.x << std::endl; } 

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contrived, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bit-wise const). Example of the former (via mutable):

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

And of the latter (via pointer):

#include <iostream> struct Foo { int x = 0; void f(Foo* foo) const { ++foo->x; } void modify_x(Foo* foo) const { f(foo); // call const member function here that actually changes the instance // ++x; // this won't work } }; int main() { Foo foo; foo.modify_x(&foo); std::cout << foo.x << std::endl; } 

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contrived, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bit-wise const). Or, even much more contrived, via a const_cast in the modifying method (this actually fits exactly your question). Example of the first (via mutable):

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

And of the second (via pointer):

#include <iostream> struct Foo { int x = 0; void f(Foo* foo) const { ++foo->x; } void modify_x(Foo* foo) const { f(foo); // call const member function here that actually changes the instance // ++x; // this won't work } }; int main() { Foo foo; foo.modify_x(&foo); std::cout << foo.x << std::endl; } 

And the last (that actually directly addresses your question) - an abomination like this, using const_cast (but you have to make sure you invoke your function on non-const objects, otherwise it leads to undefined behaviour:

#include <iostream> struct Foo { int x = 0; void f() { ++x; } void modify_x() const { const_cast<Foo*>(this)->f(); // call non-const member function } }; int main() { Foo foo; std::cout << foo.x << std::endl; foo.modify_x(); std::cout << foo.x << std::endl; } 
added 385 characters in body
Source Link
vsoftco
  • 56.9k
  • 12
  • 150
  • 269

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contrived, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bit-wise const). Example of the former (via mutable):

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

And of the latter (via pointer):

#include <iostream> struct Foo { int x = 0; void f(Foo* foo) const { ++foo->x; } void modify_x(Foo* foo) const { f(foo); // call const member function here that actually changes the instance // ++x; // this won't work } }; int main() { Foo foo; foo.modify_x(&foo); std::cout << foo.x << std::endl; } 

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contrived, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bit-wise const). Example of the former:

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

The answer is NO. The long answer is perhaps YES, provided you try to change only members marked as mutable (but not via a non-const function, that won't work at all). Or, more contrived, via a pointer to the instance (the pointer itself is may be is not changed, but what it points to can be changed without violating bit-wise const). Example of the former (via mutable):

#include <iostream> struct Foo { mutable int x = 0; // try removing mutable and you get a compile-time error void modify_x() const { ++x; } }; int main() { const Foo foo; // we make sure we can only invoke const member functions foo.modify_x(); // can modify x since it is mutable std::cout << foo.x << std::endl; } 

And of the latter (via pointer):

#include <iostream> struct Foo { int x = 0; void f(Foo* foo) const { ++foo->x; } void modify_x(Foo* foo) const { f(foo); // call const member function here that actually changes the instance // ++x; // this won't work } }; int main() { Foo foo; foo.modify_x(&foo); std::cout << foo.x << std::endl; } 
added 59 characters in body
Source Link
vsoftco
  • 56.9k
  • 12
  • 150
  • 269
Loading
added 59 characters in body
Source Link
vsoftco
  • 56.9k
  • 12
  • 150
  • 269
Loading
Source Link
vsoftco
  • 56.9k
  • 12
  • 150
  • 269
Loading