gpt4 book ai didi

c++ - 在函数中重载运算符而不写入主体 2 次

转载 作者:行者123 更新时间:2023-12-02 20:12:23 26 4
gpt4 key购买 nike

目前,我有这个代码:

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);

}

关于c++ - 在函数中重载运算符而不写入主体 2 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60617129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com