/**********C++ Primer Study*************/
#include <iostream>
using namespace std;
/*
Value behavior even though HasPtr has a pointer member
Each time we copy a HasPtr object,we make a new copy of the
underlying int object to which ptr point
*/
class HasPtr
{
public:
//constructor
HasPtr(const int &p,const int i):ptr(new int(p)),val(i){}
// copy constructor
HasPtr(const HasPtr &orig):ptr(new int(*orig.ptr)),val(orig.val)
{
}
// destructor
~HasPtr()
{
cout<<"Now delete "<<ptr<<" points.."<<endl;
delete ptr;
}
int get_ptr_val() const {return *ptr;}
void set_ptr_val(int i) {*ptr=i;}
int get_val() const {return val;}
void set_val(int i) {val=i;}
int *get_ptr() const {return ptr;}
private:
int *ptr;
int val;
};
int main(void)
{
int i=1,a=2;
HasPtr hp(i,a);
HasPtr hp1(hp);
cout<<"hp.ptr address:"<<hp.get_ptr()<<endl;
cout<<"hp1.ptr address:"<<hp1.get_ptr()<<endl;
return 0;
}