C ++函数动态数据类型定义 [英] C++ function dynamic data type definition
本文介绍了C ++函数动态数据类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中,当定义一个接受一个参数的函数时,必须定义该变量的数据类型:
in C++, when you define a function which takes one argument, you have to define the data type of that variable:
void makeProccess(int request)
但是,我想实现一个函数,采用静态定义的整数类型。
However, I want to implement a function which takes different data types rather taking statically defined integer type.
void makeProccess(anyType request)
如何设计一个这样的进程?任何想法?
How can I design a proccess like this, any idea?
谢谢。
推荐答案
使用范本:
template <typename T>
void makeProcess(T request) {
// request is of type "T", which can vary
cout << "request: " << request;
}
另一个好处是,你可以专门研究:
An additional benefit, is you can specialize it:
template <>
void makeProcess(string request) {
cout << "This is special handling for a string request: " << request;
}
这篇关于C ++函数动态数据类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文