我有一些代码分配给这样的CRTP类:
IChunkWindow<WindowDerived, IChunk<ChunkDerived>> editWindow = chunkWindow.create(
chunkWindow.getOrigin().getX() + x / ChunkDimensions::MAX_X,
chunkWindow.getOrigin().getZ() + z / ChunkDimensions::MAX_Z,
chunkWindow);
这是CRTP类:
template <class Derived, class T>
struct IChunkWindow : public IWindow<IChunkWindow<Derived,T>, T> {
Point3D const& getOrigin() const {
return static_cast<Derived const*>(this)->getOrigin();
}
void setOrigin(Point3D const& origin) {
this->origin = origin;
static_cast<Derived*>(this)->setOrigin(origin);
}
T* getChunk(int cx, int cz) {
return static_cast<Derived*>(this)->getChunk(cx,cz);
}
void setChunk(int cx, int cz, T* ptr) {
static_cast<Derived*>(this)->setChunk(cx,cz,ptr);
}
unsigned char getTerrainBlock(int x, int y, int z) const {
return static_cast<Derived const*>(this)->getTerrainBlock(x,y,z);
}
void setTerrainBlock(int x, int y, int z, unsigned char value) {
static_cast<Derived*>(this)->setTerrainBlock(x,y,z,value);
}
unsigned char getLightBlock(int x, int y, int z) const {
return static_cast<Derived const*>(this)->getLightBlock(x,y,z);
}
void setLightBlock(int x, int y, int z, unsigned char value) {
static_cast<Derived*>(this)->setLightBlock(x,y,z,value);
}
unsigned char getMetaBlock(int x, int y, int z) const {
return static_cast<Derived const*>(this)->getMetaBlock(x,y,z);
}
void setMetaBlock(int x, int y, int z, unsigned char value) {
static_cast<Derived*>(this)->setMetaBlock(x,y,z,value);
}
bool isChunkDirty(int cx, int cz) {
return static_cast<Derived*>(this)->isChunkDirty(cx,cz);
}
bool inBounds(int x, int y, int z) {
return static_cast<Derived*>(this)->inBounds(x,y,z);
}
bool inBounds(int a_cx, int a_cz) {
return static_cast<Derived*>(this)->inBounds(a_cx, a_cz);
}
void insert(int a_x, int a_y, int a_z, unsigned char blockType) {
static_cast<Derived*>(this)->insert(a_x,a_y,a_z,blockType);
}
void remove(int a_x, int a_y, int a_z) {
static_cast<Derived*>(this)->remove(a_x, a_y, a_z);
}
std::vector<BlockPosition> waterPhysics(int a_x, int a_y, int a_z, std::vector<BlockPosition> const& waterSources) {
static_cast<Derived*>(this)->waterPhysics(a_x,a_y,a_z, waterSources);
}
IChunkWindow<Derived, T> create(int a_cx, int a_cz, IChunkWindow<Derived, T> & otherWindow) {
return static_cast<Derived*>(this)->create(a_cx, a_cz, otherWindow);
}
};
我可以在editWindow上进行方法调用,但是基类的私有数据(未显示)是垃圾,所以看起来分配失败了。我是否需要覆盖IChunkWindow类中的赋值运算符才能使其生效?