#include<iostream>
#include<string>
#include<memory>//智能指针
using namespace std;
class Resouce{
public:
Resouce(){
cout << "Resouce()" << endl;
}
~Resouce(){
cout << "~Resouce()" << endl;
}
void dosomething(){
cout << "dosomething" << endl;
}
};
class Base{
public:
void fun(){
auto_ptr<Resouce>raii(new Resouce);/*RAII 资源取得时机便是初始化时机*/
}
private:
};
class Derived :public Base{
public:
//..
Derived(){}
private:
int m_i;
};
void CAPI(const Resouce*pi){
cout << "do something" << endl;
}
void test(){
auto_ptr<Resouce>r1(new Resouce);
auto_ptr<Resouce>r2(new Resouce);
shared_ptr<Resouce>r3(new Resouce);
shared_ptr<Resouce>r4(new Resouce);
// CAPI(r4);//r4的类型为shared_ptr<Investment>的对象
CAPI(r4.get());//shared_ptr auto_ptr都提供了一个get成员函数,用来执行显示转换,也就是它返回智能指针内部的原始指针(的复件)
}
//----------------------------
class FontHandle{
};
void releaseFont(FontHandle fh){}
FontHandle getFont(){
FontHandle a;
return a;
}
class Font{//RAII class
public:
Font(){}
explicit Font(FontHandle fh) :f(fh){
}
~Font(){
releaseFont(f);
}
//如果有大量与字体相关的CAPI,将Fond对象转换成FontHandle将很频繁,可以提供一个显示转换函数像shared_ptr 那样
FontHandle get()const{//显示转换函数
return f;
}
operator FontHandle()const{//隐式类型转换
return f;
}
private:
FontHandle f;
};
int newFontSize;
void changeFontSize(FontHandle f, int newSize){
}
void test2(){
Font f;
changeFontSize(f.get(), newFontSize);
changeFontSize(f ,newFontSize);
}
int main(){
Base b;
b.fun();
test();
//----------------------
system("pause");
return 0;
}