我有一个功能:
--- showme.cpp
void showme () {
std::cout <<"I am " << T << endl;
}
T is a parameter that should come with a #define
- top.cpp
int main (int argc, char** argv) {
// I want to call showme() with different "T" values defined by #define
#define T 5
showme();
#define T 6
showme();
}
我不确定是否有更好/更有效的方法。
答案 0 :(得分:3)
根据评论中的解释,您真正想要的是template
函数,因此您的示例应更改为:
#include <iostream>
template<size_t T> //or int or whatever other type you really need to use
void showme () {
std::cout << "I am " << T << std::endl;
}
int main (int argc, char** argv) {
showme<5>();
showme<6>();
}
答案 1 :(得分:1)
您应该更改函数以获取参数。调用函数时可以使用宏。
void showme (int T) {
std::cout <<"I am " << T << endl;
}
int main (int argc, char** argv) {
// I want to call showme() with different "T" values defined by #define
#define T 5
showme(T);
#define T 6
showme(T);
}