我编写了用于设置顶点属性的帮助函数。正如你所看到的,3个函数基本上是相同的,只是每个函数都设置了不同的顶点成员变量。由于它违背了DRY原则,看起来很丑,所以我想将它们结合起来。避免每个成员变量的重复函数
void setPositions(const int offset, const sf::Vector2f (&positions)[vertexCount]) {
int index = ensureSizeAndGetIndex(offset);
for(int i = 0; i < vertexCount; i++) {
vertices[index + i].position = positions[i];
}
}
void setTexCoords(const int offset, const sf::Vector2f (&texCoords)[vertexCount]) {
int index = ensureSizeAndGetIndex(offset);
for(int i = 0; i < vertexCount; i++) {
vertices[index + i].texCoords = texCoords[i];
}
}
void setColors(const int offset, const sf::Color (&colors)[vertexCount]) {
int index = ensureSizeAndGetIndex(offset);
for(int i = 0; i < vertexCount; i++) {
vertices[index + i].color = colors[i];
}
}
事情我考虑:
-
因为他们不处理成员变量
- 模板就不会在这里工作
- 我可以传递一个布尔标志结合了前两个函数其中变量的使用。但这对第三个功能无济于事。
- 我可以添加指针顶点类的成员变量和枚举可供选择,但这将是太多(性能)开销
- Lambdas也许,元编程也许?
只是会对这里最干净的解决方案感兴趣。
为什么你认为模板不能处理成员变量指针? –
标识符不能用作模板参数。我正是这个意思。 – MorbZ
你的意思是[this](https://wandbox.org/permlink/W1aA6GDB0T0ugbS3)? –