我有一个用于OpenGL缓冲区的C++类,它有许多setData()
函数来说明缓冲区可能包含的不同类型的数据,例如整型:如何避免使用不同类型C++的多个函数执行相同的操作
void Buffer::setData(int* data)
{
//Bind the buffer
bind();
//Input the data into the buffer based on the type
glBufferData(type, sizeof(int) * size, data, GL_DYNAMIC_DRAW);
}
这是功能则仅改变的是sizeof(int)
变得sizeof(<other type>)
我想知道是否有办法解决事情的每一个版本一样吗?我考虑过的一种可能性是泛型类型变量,如var?我知道var本身并不存在于C++中,但是有一些等价的吗?
模板是否工作? – alphacentauri 2014-09-04 16:28:47
不知道如何使用模板我是一个C++新手,我会研究它:) – DomAyre 2014-09-04 16:29:53