strcpy (mChars, rhs.c_str ());
mString = rhs.c_str ();
return *this;
}
// Supply operator methods for converting the JNIString to a string
// or char*, making it easy to pass JNIString arguments to functions
// that require string or char* parameters.
string & GetString() { return mString; }
operator string() { return mString; }
operator const char* () { return mString.c_str (); }
operator jstring() { return mJstr; }
private:
JNIEnv* mEnv; // The enviroment pointer for this native method.
jstring mJstr; // A copy of the jstring object that this JNIString represents
char* mChars; // Pointer to a ANSI code page char array
string mString; // string buffer for holding the "value" of this instance (ANSI code page)
};
后者除了将面向UTF编码改成了面向ANSI编码外,还去掉了operator =(const char* ptr)的定义,因为operator =(const string& rhs)可以在需要的时候替代前者而无需任何额外编码。(因为按照C++规范,const reference可以自动转换,详见本人另一文章《关于const reference的几点说明》http://blog.vckbase.com/billdavid/archive/2004/11/11/1453.html)