2016-07-12 2 views
2

:상수 포인터 참조

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 통해 액세스 될 때 포인터 CONST 실제 변수를 일정하게 할 수있다?

답변

2

에 한번 선언 var_ptrconst int로하고 var_ptr_refconst 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; 
}; 
+0

내가 얻을 오류'생성자 라인에 대한 임시 variable' 참조 멤버 'var_ptr_ref를'바인딩. – user1135541

+0

@ user1135541 잘 작동합니다. http://ideone.com/PqKXDc –

+0

@ user1135541 적어도 VS2015는 컴파일하는 것이 행복합니다. – AlexD