c++ - 在函数中重载运算符而不编写正文 2 次
问题描述
目前,我有这个代码:
void writeLog(__FlashStringHelper* mes){
Serial.println(mes);
if(!logfile){
logfile = SD.open(logpath,FILE_WRITE);
if(!logfile) return;
}
logfile.write(mes);
}
void writeLog(char* mes){
Serial.println(mes);
if(!logfile){
logfile = SD.open(logpath,FILE_WRITE);
if(!logfile) return;
}
logfile.write(mes);
}
有没有办法重载一个函数来接受 char* 和 __FlashStringHelper* 而无需写入正文 2 次?
谢谢!
解决方案
如果你可以使用模板试试这个:
template <typename T>
void writeLog(T mes) {
Serial.println(mes);
if (!logfile) {
logfile = SD.open(logpath, FILE_WRITE);
if (!logfile) return;
}
logfile.write(mes);
}
推荐阅读
- java - 在 oracle 中引用另一个模式中的表
- google-apps-script - 使用“onOpen(e)”调用函数时出错
- excel-formula - 返回列表中的第二个(和后续)唯一名称
- c# - 类库中的 ASP.NET Core DI?
- python-3.x - for_log 生成器不返回任何消息
- android - 材料设计排版和小吃店
- unity3d - 来回切换时未正确加载场景
- javascript - 事件侦听器不适用于 javascript 中的按钮
- excel - 在 Excel 形状内连接
- javascript - 使用 javascript 从 JSON 文件中获取数据并将其插入到下拉列表中