level 9
#ifndef CACHEDOBJ_CPP_INCLUDED
#define CACHEDOBJ_CPP_INCLUDED
#include "CachedObj.h"
//定义静态数据成员
template <
class T> std::allocator<T> CachedObj<T>::alloc_mem;
template <
class T> T *CachedObj<T>::freeStore = 0;
template <
class T>
const std::size_t CachedObj<T>::chunk = 24;
template <
class T>
void *CachedObj<T>::
operator new(size_t sz)
{
//
new只创建T类型对象,不能用于创建T的派生类的对象
//因此需要检查所要求分配内存的数量
if(sz!=
sizeof(T))
{
throw std::runtime_error("CachedObj:wrong size object in
operator new");
}
if(!freeStore)
{
//自由列表为空
//使用allocator对象分配chunk个T类型的未构造对象
T *array = alloc_mem.allocate(chunk);
//设置新分配空间中每个对象的next指针
for(size_t i=0;i!=chunk;++i)
{
add_to_freelist(&array[i]);
}
}
T *p = freeStore;
freeStore = freeStore->CachedObj<T>::next;
return p;//T的构造函数将构造对象的T类部分
}
template <
class T>
void CachedObj<T>::
operator delete(
void *p,size_t)
{
if(p!=0)
{
add_to_freelist(
static_cast<T *>(p));
}
}
//将对象放在自由列表的头端
template <
class T>
void CachedObj<T>::add_to_freelist(T *p)
{
p->CachedObj<T>::next = freeStore;
freeStore = p;
}
#endif
2013年10月22日 07点10分
2
level 9
<!DOCTYPE html>
<html>
<head>
<title>arguments</title>
<script>
function format(string)
{
var args = arguments;
var pattern =
new RegExp("%([1-"+arguments.length+"])","g");
return String(string).replace(pattern,function(match,index) {
return args[index];
});
}
</script>
</head>
<body>
<script>
document.writeln(format("Hello %1,I am %2.","Jim","Tom"));
document.writeln("What
this looks like?");
</script>
</body>
</html>
2013年10月22日 07点10分
3