Demonstration des Policy Pattern
#include <iostream>
using namespace std;
template <class T>
class PlacementNewCreator {
public:
static T *create() {
void *buf = malloc(sizeof(T));
if (!buf) return 0;
return new(buf) T;
}
};
template <class T>
class NewCreator {
public:
static T *create() {
return new T;
}
};
struct Vertex
{
float x, y, z;
};
template
<
class CreationPolicy
>
class VertexBuffer : public CreationPolicy
{
public:
void AddVertex()
{
Vertex* v = CreationPolicy::create();
}
};
typedef VertexBuffer< NewCreator<Vertex> > MyVertexBuffer;
void main()
{
MyVertexBuffer x;
x.AddVertex();
system("pause");
}
2 Kommentare zum Snippet