#include <iostream>
#include <string>
using namespace std;
class HasPtr{
public:
HasPtr(const int &p,int i):ptr(new int(p)) ,val(i) {}
HasPtr(const HasPtr & orgi): ptr(new int(*orgi.ptr)),val(orgi.val) {}
HasPtr& operator= (const HasPtr& rhs)
{
*ptr = *rhs.ptr;
val = rhs.val;
return *this;
}
~HasPtr(){ delete ptr;}
void setPtr(int* p){ ptr = p;}
void setVal(int v) { val = v;}
int *getPtr()const {return ptr;}
int getVal() { return val;}
private:
int* ptr;
int val;
};
int main()
{
int pt =2;
HasPtr hp(pt,pt);
HasPtr p1(hp);
return 0;
#include <string>
using namespace std;
class HasPtr{
public:
HasPtr(const int &p,int i):ptr(new int(p)) ,val(i) {}
HasPtr(const HasPtr & orgi): ptr(new int(*orgi.ptr)),val(orgi.val) {}
HasPtr& operator= (const HasPtr& rhs)
{
*ptr = *rhs.ptr;
val = rhs.val;
return *this;
}
~HasPtr(){ delete ptr;}
void setPtr(int* p){ ptr = p;}
void setVal(int v) { val = v;}
int *getPtr()const {return ptr;}
int getVal() { return val;}
private:
int* ptr;
int val;
};
int main()
{
int pt =2;
HasPtr hp(pt,pt);
HasPtr p1(hp);
return 0;
}
经测试在linux平台下面可以正常运行