python c++ 混合編程中python調用c++string返回類型的函數,python中返回爲數字的解決辦法

本隨筆解決 Python使用ctypes 調用c++dll 字符串返回類型函數,在python中顯示爲數字;原文解決方案見so:python

https://stackoverflow.com/questions/12500069/ctypes-how-to-pass-string-from-python-to-c-function-and-how-to-return-string/12500326#12500326c++

解決方案以下:函數

1.      聽說無關pythonctypes的事。spa

2.      C++定義導出函數的時候,須要使用const char *,而不能使用string返回類型,如:orm

extern "C"ci

const char *return_string(char* name){字符串

    cout<<strlen(name)<<endl;get

    cout<<name<<endl;string

    static string s = "hello ";io

    s += name;

    return s.c_str();

}

3.      上面的函數定義也看到了,在函數體中使用string臨時變量的話,須要static string str這樣的形式定義,聲明爲靜態變量。

4.      string類型臨時變量賦值的時候,必須使用+=,而不是=

 

5.      最後往出傳遞的話,使用s.c_str()作個轉換。

相關文章
相關標籤/搜索