我想擴展C++字符串類返回新實例,返回子類的引用(而不是父串參考),但是這個代碼摘錄...正確的方式從C++字符串的子類
#include <string>
using namespace std;
class mystring : public string
{
public:
mystring& left(int cnt)
{ return (mystring&)mystring(substr(0,cnt));
}
};
產生這種VS8編譯器錯誤:
error C2440: '' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'mystring'
什麼是聲明的MyString正確的方式::左(),所以編譯器將停止抱怨,希望也消除了投?
請解釋爲什麼你認爲你需要繼承的std :: string這樣。 – 2009-12-23 20:43:39
我正在重用使用MFC CString的代碼,該代碼使用了C++字符串中沒有的Left,Right和其他函數 – Mike 2009-12-23 20:56:00
std :: string和MFC CString不兼容。你最好的辦法是把像mfc.Left(3)這樣的東西變成像 Left(stds,3)這樣的免費函數。 – 2009-12-23 21:02:12