I saw this code in the book "C++ High Performance" by Bjorn Andrist and Viktor Sehr.
The code example is actually used to show the point that "Compiles despite function being declared const
!", and I am aware of this. However, I have not seen int* ptr_{};
and Foo(int* ptr):ptr_{ptr}{}
before this point. What are these two pieces of code doing, especifically Foo(int* ptr):ptr_{ptr}{}
?
The entire code snippet used as an example was this:
class Foo {
public:
Foo(int* ptr):ptr_{ptr}{}
auto set_ptr_val(int v) const {
*ptr_ = v; // Compiles despite function being declared const!
}
private:
int* ptr_{};
};
int main(){
auto i = 0;
const auto foo = Foo{&i};
foo.set_ptr_val(42);
}