对于以下代码:
class Foo {
private:
int var;
int* var_ptr;
public:
Foo() : var_ptr(&var), var_ptr_ref(var_ptr) {}
int*& var_ptr_ref; // Read only access to var and var_ptr
};
通过var_ptr_ref
?
答案 0 :(得分:2)
尝试将var_ptr
声明为const int
,将var_ptr_ref
声明为const int * const &
:
class Foo {
private:
int var;
const int * var_ptr;
public:
Foo() : var_ptr(&var), var_ptr_ref(var_ptr) {}
const int * const & var_ptr_ref;
};