![](/img/trans.png)
[英]How can I define a function pointer that takes the same arguments and return value as a given function?
[英]How to define a function to return a pointer to a “” value when no matching data is found?
我希望我的function()在錯誤情況下始終返回“”字符串,否則返回從無符號長整數變量轉換為字符串的字符串。
我的初始實現如下:
uint32 cfgVariable_1 = 4;
uint32 cfgVariable_2 = 1;
const char* getCfgVariable (const char* msg)
{
char* retValue = "";
if(strcmp("cfgVariable_1", msg)==0)
{
// Since I want my function to return a const char* and returning uint32_t is not an option
sprintf((retValue), "%lu", cfgVariable_1);
return (const char*)retValue;
}
else if(strcmp("cfgVariable_2", msg)==0)
{
// Since I want my function to return a const char* and returning uint32_t is not an option
sprintf((retValue), "%lu", cfgVariable_2);
return (const char*)retValue;
}
else
{
//error
}
return (const char*) retValue;
}
當在不同的實例上調用該函數以獲取cfgVariables時,我期望我的函數getCfgVariable()在找不到匹配項時在錯誤情況下返回“”。 代碼中的某處:
const char* CfgValue = NULL;
CfgValue = getCfgVariable("cfgVariable_1");
這里CfgValue指向包含4的位置
后來
const char* CfgValue = NULL;
CfgValue = getCfgVariable("cfgVariable_3");
我希望得到一個“”,但我得到4個(CfgValue獲取與以前相同的地址)。
由我實施的修復有效,但是我無法理解其背后的邏輯,請修復:
const char* getCfgVariable (const char* msg)
{
const char* defValue = "";
char* retValue = "\0";
if(strcmp("cfgVariable_1", msg)==0)
{
// Since I want my function to return a const char* and returning uint32_t is not an option
sprintf((retValue), "%lu", cfgVariable_1);
return (const char*)retValue;
}
else if(strcmp("cfgVariable_2", msg)==0)
{
// Since I want my function to return a const char* and returning uint32_t is not an option
sprintf((retValue), "%lu", cfgVariable_2);
return (const char*)retValue;
}
else
{
//error
}
return defValue;
}
我在調試過程中看到defValue和retValue被指向兩個不會被覆蓋的不同位置。 defValue始終使用“”初始化時指向相同的地址,而retValue使用“ \\ 0”初始化時指向不同的地址。 誰能解釋這個背后的邏輯? 我的用例是否有更好的實現?
考慮評論后的解決方案:
const char* getCfgVariable (const char* msg)
{
const char* retValue = "";
std::ostringstream oss;
if(!strcmp("cfgVariable_1", msg))
{
oss << cfgVariable_1;
}
else if(!strcmp("cfgVariable_2", msg))
{
oss << cfgVariable_2;
}
else
{
//error
return retValue;
}
const std::string tmp = oss.str();
retValue = tmp.c_str();
return retValue;
}
到目前為止,感謝您的評論,此解決方案仍然可以接受進一步的改進建議。
Constexpr字符串,例如“ \\ 0”,“”,“ cfgVariable_1”等。這些是內存中的常量字符串,已編譯為最終的可執行文件。 試圖將值寫入這些字符串非常危險! 在舊樣式C中,您必須使用malloc分配一些內存以用於您的字符串。 這是在實踐中處理的真正痛苦(對於學習C ++的人來說並不理想)。
一個簡單得多的解決方案是開始使用C ++字符串對象std :: string(它為您處理所有動態內存分配!)。 這樣可以將您的問題減少到更簡單一些(最重要的是,更安全!):
#include <string>
#include <sstream>
std::string getCfgVariable (const char* const msg)
{
std::ostringstream oss;
if(!strcmp("cfgVariable_1", msg))
{
oss << cfgVariable_1;
}
else
if(!strcmp("cfgVariable_2", msg))
{
oss << cfgVariable_2;
}
return oss.str();
}
在C中執行此操作,您有2個選擇。 為返回的字符串分配內存,或使用始終可用的靜態緩沖區(此示例將執行此操作)。
uint32 cfgVariable_1 = 4;
uint32 cfgVariable_2 = 1;
const char* getCfgVariable (const char* msg)
{
static char retValue[32] = {0};
if(strcmp("cfgVariable_1", msg)==0)
{
// Since I want my function to return a const char* and returning uint32_t is not an option
sprintf(retValue, "%lu", cfgVariable_1);
return retValue;
}
else if(strcmp("cfgVariable_2", msg)==0)
{
// Since I want my function to return a const char* and returning uint32_t is not an option
sprintf(retValue, "%lu", cfgVariable_2);
return retValue;
}
else
{
//error
}
return retValue;
}
但是,因為現在retValue是固定在內存中的數組,所以返回的字符串僅在下一次調用getCfgVariable之前才有效,這可能有點奇怪。
const char* A = getCfgVariable("cfgVariable_1");
printf("%s\n", A); // prints '4'
const char* B = getCfgVariable("cfgVariable_2");
printf("%s\n", B); // prints '1'
printf("%s\n", A); // now this will print '1', and not '4'.
const char* C = getCfgVariable("anythingElse");
printf("%s\n", C); // prints ""
printf("%s\n", B); // prints ""
printf("%s\n", A); // aso prints ""
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.