我編寫了用於設置頂點屬性的幫助函數。正如你所看到的,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)? –