概述
头文件
boost/core/checked_delete.hpp
作用
delete 对象指针,或 对象数组指针,如果对象是不完整定义类型或者空指针,则 编译期间 出错。
何为不完整类型定义,就是只有 类型前置声明,没有引入头文件。
举例
//base.h
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{}
virtual ~Base()
{
cout<<"~Base()"<<endl;
}
};
//drive.h
#include <iostream>
using namespace std;
class Drive : public Base
{
public:
Drive()
{}
virtual ~Drive()
{
cout<<"~Drive()"<<endl;
}
};
//deleteobject.h
class Base;
class DeleteObject
{
public:
DeleteObject()
{}
~DeleteObject()
{
}
void deleteObj(Base* base);
};
//deleteobject.cpp
void DeleteObject::deleteObj(Base* base)
{
//并不会 delete Base对象,Base并没有执行。
//因为 deleteobject.cpp 并没有 引言 base.h
delete base;
}
//main.cpp
#include "drive.h"
#include "deleteobject.h"
int main(int argc, char* [])
{
Base* base = new Drive();
DeleteObject deOb;
//
deOb.deleteObj(base);
return 0;
}
为了 避免 以上情况 发生,就需要 使用 checked_delete
举例
删除 空指针
#include <boost/checked_delete.hpp> // for checked_delete
// This program demonstrates compiler errors when trying to delete an
// incomplete type.
namespace
{
class Incomplete;
}
int main()
{
Incomplete * p = 0;
boost::checked_delete(p); // should cause compile time error
return 0;
} // main
删除 空指针数组
#include <boost/checked_delete.hpp> // for checked_delete
// This program demonstrates compiler errors when trying to delete an
// incomplete type.
namespace
{
class Incomplete;
}
int main()
{
Incomplete * p = 0;
boost::checked_array_delete(p); // should cause compile time error
return 0;
} // main
正常删除
#include <boost/core/checked_delete.hpp>
#include <boost/core/lightweight_test.hpp>
struct X
{
static int instances;
X()
{
++instances;
}
~X()
{
--instances;
}
private:
X( X const & );
X & operator=( X const & );
};
int X::instances = 0;
int main()
{
BOOST_TEST( X::instances == 0 );
{
X * p = new X;
BOOST_TEST( X::instances == 1 );
boost::checked_delete( p );
BOOST_TEST( X::instances == 0 );
}
{
X * p = new X[ 3 ];
BOOST_TEST( X::instances == 3 );
boost::checked_array_delete( p );
BOOST_TEST( X::instances == 0 );
}
return boost::report_errors();
}
源码
namespace boost
{
// verify that types are complete for increased safety
template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x;
}
template<class T> inline void checked_array_delete(T * x)
{
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete [] x;
}
template<class T> struct checked_deleter
{
typedef void result_type;
typedef T * argument_type;
void operator()(T * x) const
{
// boost:: disables ADL
boost::checked_delete(x);
}
};
template<class T> struct checked_array_deleter
{
typedef void result_type;
typedef T * argument_type;
void operator()(T * x) const
{
boost::checked_array_delete(x);
}
};
} // namespace boost
最后
以上就是飞快时光为你收集整理的Boost中Core模块的checked_delete用法头文件作用举例源码的全部内容,希望文章能够帮你解决Boost中Core模块的checked_delete用法头文件作用举例源码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复