我想创建一个对某些类型的参数执行相同操作的方法。
例如:我有一个文件,并且想要WriteToFile()
将字符串或 int 或 double 写入文档的方法。
WriteToFile( type content)
{
streamFile.open ("path\to\file.txt", fstream::in | fstream::out| fstream::app);
streamFile<<content;
}
在 C# 中我使用 T,是否可以使用 C++ 来实现它?
我想创建一个对某些类型的参数执行相同操作的方法。
例如:我有一个文件,并且想要WriteToFile()
将字符串或 int 或 double 写入文档的方法。
WriteToFile( type content)
{
streamFile.open ("path\to\file.txt", fstream::in | fstream::out| fstream::app);
streamFile<<content;
}
在 C# 中我使用 T,是否可以使用 C++ 来实现它?
C++ 中有一些模板允许您为不同的类型执行相同的逻辑:
template < typename T>
void WriteToFile( T content, File& streamFile)
{
streamFile.open ( "path\to\file.txt", fstream::in
| fstream::out| fstream::app);
streamFile << content;
}
那就是你可以创建一个函数族。