Код: Выделить всё
const
Код: Выделить всё
// header.hpp
struct Foo {
int i;
void maybeChange();
};
void work(Foo const& foo);
Код: Выделить всё
// foo.cpp
#include "header.hpp"
void Foo::maybeChange() {
/* body */
}
Код: Выделить всё
// work.cpp
#include "header.hpp"
void work(Foo const& foo) {
const_cast(foo).maybeChange();
}
Код: Выделить всё
// main.cpp
#include "header.hpp"
Foo const foo{6};
int main() {
work(foo);
}
Но, с другой стороны, http://eel.is/c++draft/dcl.type.cv#4 не показывает ни одного примера вызова не-
Код: Выделить всё
const
Код: Выделить всё
const int* ciq = new const int (3); // initialized as required
int* iq = const_cast(ciq); // cast required
*iq = 4; // undefined behavior: modifies a const object
Подробнее здесь: https://stackoverflow.com/questions/792 ... tained-via