所以我有这个函数(有 80 行):
int listPlatformInfo(..., char * foo)
{
...
for (uint32_t a = 0; a < platformCount; a++)
{
platformInfo(platforms, info, foo);
}
return 0;
}
我有 20 个不同的函数重载 platformInfo(); 有没有办法重载这个函数,唯一的变化是 foo 的数据类型,而不复制整个函数 20 次?
最佳答案
使用模板:
template<typename T>
int listPlatformInfo(..., T foo) // or T* ?
{
...
for (uint32_t a = 0; a < platformCount; a++)
{
platformInfo(platforms, info, foo);
}
return 0;
}
关于c++ - 如何有效地重载函数而不发疯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40295097/