由买买提看人间百态

topics

全部话题 - 话题: hasptr
(共0页)
y**b
发帖数: 10166
1
来自主题: Programming版 - 形参可以直接使用私有数据成员?
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
来自主题: Programming版 - a question about const reference
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
来自主题: Programming版 - a question about const reference
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.
(共0页)