-
- 加为好友
- 发送私信
- 在线聊天
- jinder22
-
- 等级:
- 可用分等级:
- 总技术分:
- 总技术分排名:
|
发表于:2008-11-21 16:34:1789楼 得分:0 |
#include <iostream> #include <typeinfo> using namespace std; class CBase { public: int *p; CBase(){} ~CBase() { cout < <"释放堆地址" < <p < <endl; delete p; } CBase(const CBase &cbase) { p=cbase.p;//这样跟默认的位拷贝没有区别 //p=new int; //*p = *(cbase.p); // 这样才是深拷贝!! } CBase(int *p) { this->p = p; } CBase &operator=(const CBase &cbase)//赋值函数 { p=cbase.p; } void print() { cout < <"对象" < <this < <":" < <"地址:" < <p < <"\t" < <"值:" < <*p < <endl; } }; int main() { //int a=8; //CBase p(&a);//错误,最后p指向了栈内存了 int *a = new int(8); int *b = new int(4); int i =1; int j =2; int *ii,*pp; *ii=2,*pp=4; cout < < *ii < < " " < < ii < < endl; cout < < *pp < < " " < < pp < < endl; //pp=NULL; //pp=ii; *pp=*ii; cout < < *ii < < " " < < ii < < endl; cout < < *pp < < " " < < pp < < endl; cout < < i < < &i; CBase p(a); p.print(); CBase p1(p); p1.print(); CBase p2(b); p2=p; p2.print(); return 0; } |
|
|
|