句柄类及其使用举例
由于c++不支持对象的动态多态,只有指针或引用支持动态多态。所以,使用一个句柄类(只包含指针数据成员)来支持对象的多态性
句柄的使用一般包含如下两种情况:对象的多态性和对象的共同计数
源代码如下:
#include <iostream>
using namespace std;
class point
{
public:
point():x_cdt(0),y_cdt(0){}
point(int x,int y):x_cdt(x),y_cdt(y){}
int get_x()
{ return x_cdt;}
int get_y()
{ return y_cdt;}
point& set_x(int x)//·µ»ØÖµÊÇpoint&ÊÇΪÁËʵÏÖÕâÑùµÄ±í´ï: p.set_x(2).set_y(4);
{
x_cdt = x;
return *this;
}
point& set_y(int y)
{
y_cdt = y;
return *this;
}
virtual point* clone()const{//cloneº¯Êý£¬ÊéP507
cout<<"»ùÀ๹Ôì"<<endl;
return new point(*this);
}
virtual void print() const//Ð麯Êý£¬ÒÔ±»¶¯Ì¬µ÷ÓÃ
{
cout<<"»ùÀà´òÓ¡"<<endl;
}
private:
int x_cdt,y_cdt;
};
class d_point:public point{ //¼Ì³ÐÀà
int d;
public:
d_point(point p):point(p)
{
d=0;
}
d_point* clone() const //ÖØÐ´cloneº¯Êý£¬ÊéP507
{
cout<<"ÅÉÉúÀ๹Ôì"<<endl;
return new d_point(*this);
}
void print()const //ÖØÐ´Ð麯Êý£¬ÒÔ±»¶¯Ì¬µ÷ÓÃ
{
cout<<"ÅÉÉúÀà´òÓ¡"<<endl;
}
};
//////////////////////////////////////////////////////////
class usecount
{
public:
friend class handle;
usecount():p(new int(1)){}
usecount(const usecount& h):p(h.p)//pºÍh.pÏàͬ£¬Òâζ×ÅpºÍh.pÖ¸ÏòͬһÄÚ´æ £¬Öص㰡£¬Í³Ò»¼ÆÊýÔÀí
{
++*p;
}
// usecount& operator=(const usecount&);
~usecount()
{
if(--*p == 0)
delete p;
}
private:
bool reattach(const usecount&h);
int* p;
};
bool usecount::reattach(const usecount&h)
{
++*h.p;
if(--*p == 0){
delete p;
p = h.p;
return true;
}
p = h.p;
return false;
}
///////////////////////////////////////////////
class handle
{
public:
handle():ptr(0){}
handle(const point &p):ptr(p.clone()){}
handle(int x,int y):ptr(new point(x,y)){}
handle(const handle& h):ptr(h.ptr),u(h.u){}
handle& operator=(const handle& h);
~handle(){
if(--(*u.p) == 0)
delete ptr;
}
int handle_count()//Á¬½Óµ½count_pointµÄÊýÄ¿
{
return *u.p;
}
void print()
{
ptr->print();
}
private:
point* ptr;
usecount u;//¸Ä³ÉÕâ¸ö
};
handle& handle::operator=(const handle& h)
{
if(u.reattach(h.u))
delete ptr;
ptr = h.ptr;
return *this;
}
int main( )
{
point p1(1,1);
d_point dp1(p1);
cout<<"ÓûùÀà³õʼ»¯!"<<endl;
//count_point cp1(p1);
handle h1(p1);
handle h2(h1);
handle h3(h1);
h1.print();
cout<<h1.handle_count()<<endl;
////////////////////////////////////
cout<<"ÓÃÅÉÉúÀà³õʼ»¯!"<<endl;
handle h11(dp1); //¿ÉÒÔ½ÓÊÜÅÉÉúÀà¶ÔÏó£¨Í¨¹ý»ùÀàÖ¸Õ붯̬°ó¶¨£©
handle h12(h11);
handle h13(h11);
h11.print(); //¶ÔÏóµÄ¶à̬ÐÔ
cout<<h11.handle_count()<<endl;
///////////////////////////////////
int a;
cin>>a;
return 0;
}
/*class handle
{
public:
handle();
handle(int x,int y);
handle(const point&);
handle(const handle&);
handle& operator=(const handle&);
~handle();
private:
point* operator->();
}*/
//////////////////////////////////////
////////////////////////////////////////////////
/*
ÎÒÃÇÔÝʱûÓаÑset_x£¨£©ÕâÑùµÄ³ÉÔ±ÏȼÙÈçµ½ÕâÀΪÁ˼ò±ã¡£
µ±ÎÒÃÇдÍêÖ®ºó£¬ÎÒÃÇÓÖÓöµ½ÁËʱ¿ÌÀ§ÈÅÎÒÃǵÄÎÊÌ⣬¾ÍÊǶÔÏóµÄɾ³ý£¬¼ÈÈ»ÎÒÃÇÔÊÐí°ÑÒ»¸ö¶ÔÏó°ó¶¨µ½¶à¸öhandleÉÏ£¬
µ±ÆäÖеÄÒ»¸öhandleɾ³ýµÄʱºò£¬Õâ¸ö¶ÔÏó»áÔõôÑùÄØ£¿Õâ¸öÎÊÌâÊÇÒª½â¾öµÄ¡£ÕâÀÎÒÃÇÄØÒýÈëÁËÒýÓüÆÊýµÄ¸ÅÄ
ºÜÏÔÈ»£¬ÎÒÃDz»ÄܰÑÕâ¸öÒýÓüÆÊý·Åµ½ÎÒÃǵÄhandleÀàÖУ¬ÒòΪһµ©ÎÒÃÇÕâô×öÁË£¬µ±ÎÒÃÇÓÃÒ»¸öhandle°ó¶¨µ½Ò»¸ö¶ÔÏóµÄʱºò£¬
Ëû±ØÐëÖªµÀÆäËûËùÓа󶨵½Õâ¸ö¶ÔÏóµÄhandleµÄÒýÓüÆÊý£¬È»ºó¸üÐÂÕâ¸ö¼ÆÊý£¬Õâ·Ç³£À§ÄÑ¡£
ÄÇôÈç¹ûÎÒÃǰÑÕâ¸öÒýÓüÆÊý·Åµ½¶ÔÏó±¾ÉíÄØ£¿ÉÔ΢˼¿¼Ò»Ï¾ÍÖªµÀÒ²ÊDz»ºÏÀíµÄ£¬ÕâÑù×öÒâζ×ÅÎÒÃÇÿ´Î¶¼Òª¸Äд¶ÔÏó¡£
ËùÒÔ£¬×ÛÉÏ¿¼ÂÇ£¬ÎÒÃǾö¶¨Ð´Ò»¸öеÄÀàÀ´´æ·ÅÕâ¸öÒýÓüÆÊý£¬Õâ¸öÀàÊÇÍêȫΪÁ˼¼ÊõµÄʵÏÖÉè¼ÆµÄ£¬
ËùÒÔÎÒÃǰÑËûµÄËùÓгÉÔ±ÉèÖóÉprivate£¬¶øÈÃhandle³ÉΪËûµÄÓÑÔªÀ࣬¾ßÌåÊÇÕâÑùµÄ£º
class count_point//³õʼ°æ±¾
{
friend class handle;
point p;
int count;
count_point();
count_point(int x,int y);
count_point(const count_point&);
};*/

浙公网安备 33010602011771号