y**b 发帖数: 10166 | 1 class HasPtr {
public:
HasPtr(const int &p, int i): ptr(new int(p)), val(i) {}
HasPtr(const HasPtr &orig):
ptr(new int (*orig.ptr)), val(orig.val) { }
HasPtr& operator=(const HasPtr&);
~HasPtr() { delete ptr; }
int get_ptr_val() const { return *ptr; }
int get_int() const { return val; }
void set_ptr(int *p) { ptr = p; }
void set_int(int i) { val = i; }
int *get_ptr() const { return ptr; }
void set_ptr_val(int p) const { *ptr = p; }
... 阅读全帖 |
|
d*********1 发帖数: 25 | 2 In the 4th version of C++ primer, regarding smart pointer, it invokes the
HasPtr as const reference, how could it changes the value of rhs?
++rhs.ptr->use
and --ptr->use |
|
d*********1 发帖数: 25 | 3 I have done a test that
rhs.ptr can't be changed , eg rhs.ptr=NULL //illegal
so, in this case, the pointers in the class of HasPtr are defined as:
datatype * const pointer;
so the value of pointer can be changed.
it is a bit of tricky. |
|