1 Star 0 Fork 0

小风code/智能指针CPP

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.cpp 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
小风code 提交于 2018-11-18 19:00 +08:00 . 添加项目文件。
#include <iostream>
#include "SharePtr.h"
#include <vector>
#include <Windows.h>
using namespace std;
class A
{
public:
A()
{
cout << "init" << endl;
}
~A()
{
cout << "destory" << endl;
}
};
class B
{
public:
B(int a)
{
cout << "init b " << a << endl;
}
~B()
{
cout << "destory b " << endl;
}
};
class Base
{
public:
Base() { cout << "init base" << endl; }
virtual ~Base() { cout << "destory base" << endl; }
void basePrintf()
{
cout << __FUNCTION__ << endl;
}
virtual void printf()
{
cout << __FUNCTION__ "Base"<< endl;
}
};
class Test
:public Base
{
public:
Test() { cout << "int Test" << endl; }
virtual ~Test() { cout << "destory Test " << endl; }
void TestPrintf()
{
cout << __FUNCTION__ << endl;
}
void printf()
{
cout << __FUNCTION__ "test" << endl;
}
};
void func()
{
SharePtr<Base> p = staticPointerCast<Base>(makeSharePtr<Test>());
p->basePrintf();
p->printf();
SharePtr<Test> t = dynamicPointerCast<Test>(p);
if (t != nullptr)
{
t->TestPrintf();
p->printf();
}
}
void run(SharePtr<Base> v)
{
while (true)
{
Sleep(100);
}
}
int main()
{
func();
vector<SharePtr<Base>> ver;
ver.push_back(staticPointerCast<Base>(makeSharePtr<Test>()));
SharePtr<Base> p = makeSharePtr<Base>();
ver.push_back(p);
ver.clear();
p.reset();
const SharePtr<Base> v = makeSharePtr<Base>();
SharePtr<Base> b = constPointerCast<Base>(v);
b->basePrintf();
run(p);
//std::make_shared()
// std::static_pointer_cast<>
/*SharePtr<A> p(new A());
std::vector<SharePtr<A>> ver;
ver.push_back(p);
ver.push_back(SharePtr<A>(new A()));
ver.clear();
SharePtr<A> pp = makeSharePtr<A>();
func();
SharePtr<B> pb = makeSharePtr<B>(123);
cout << "----------" << endl;
*/
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/xfcode/smart_pointer_cpp.git
git@gitee.com:xfcode/smart_pointer_cpp.git
xfcode
smart_pointer_cpp
智能指针CPP
master

搜索帮助