wxChar 與 wxString 的關係

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
HeMason
Experienced Solver
Experienced Solver
Posts: 73
Joined: Tue Jun 30, 2009 10:07 am
Location: Taiwan
Contact:

wxChar 與 wxString 的關係

Post by HeMason »

請問前輩們:

Code: Select all

wxString text="This is a test." ;
傳入函示

Code: Select all

void FunctionName( const wxString & text)
{
 wxChar *p= &text[0] ;
}
*p 就是第一個字母 'T'
*(p++) 就是第二個字母 'h'
是嗎?

我試用原先 C 的方是思考,但是這在實際執行時,卻不是這樣?!
*(p++) 竟然是 '\0' ?!

要是我用 p = text.c_str() ;
編譯就現錯誤:
error C2440: '型別轉換' : 無法由 'wxCStrData' 轉換為 'wxChar *'

而我看 src 嚇得一些原碼,卻都這樣用,也沒事!

請問為什麼?
「漢書文書處理系統」作者,在這向大家學習。
MyBlog 梅僧山房
Satervalley
Knows some wx things
Knows some wx things
Posts: 47
Joined: Fri Dec 14, 2007 1:10 am

Post by Satervalley »

一般为了让你的程序无需更改就可在 ansi 或 unicode 模式下转换,需要定义一个通用的char 类型。mfc 下是TCHAR,wxWidgets 下就是wxChar了,一般是一个类型定义,在 ansi 模式下,wxChar 等同于单字节的 char,unicode 模式下,则定义为 w_char,即 16位的宽字符。为了编程方便,还有一个宏 wxT,要记住用。即
不要写 wxString str = "aaaaaa";
而要写 wxString str = wxT("aaaaaa");
HeMason
Experienced Solver
Experienced Solver
Posts: 73
Joined: Tue Jun 30, 2009 10:07 am
Location: Taiwan
Contact:

Post by HeMason »

Satervalley wrote:一般为了让你的程序无需更改就可在 ansi 或 unicode 模式下转换,需要定义一个通用的char 类型。mfc 下是TCHAR,wxWidgets 下就是wxChar了,一般是一个类型定义,在 ansi 模式下,wxChar 等同于单字节的 char,unicode 模式下,则定义为 w_char,即 16位的宽字符。为了编程方便,还有一个宏 wxT,要记住用。即
不要写 wxString str = "aaaaaa";
而要写 wxString str = wxT("aaaaaa");
感謝!
您說的小弟倒是有在用,舉例時忘了加上。
上面小弟的疑問,不知道您可以回答嗎?
感謝!
「漢書文書處理系統」作者,在這向大家學習。
MyBlog 梅僧山房
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Re: wxChar 與 wxString 的關係

Post by samsam598 »

HeMason wrote: {
 wxChar *p= &text[0] ;
}
这个有点怪怪的。
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
HeMason
Experienced Solver
Experienced Solver
Posts: 73
Joined: Tue Jun 30, 2009 10:07 am
Location: Taiwan
Contact:

Re: wxChar 與 wxString 的關係

Post by HeMason »

samsam598 wrote:
HeMason wrote: {
 wxChar *p= &text[0] ;
}
这个有点怪怪的。
是怪怪的。
所以小弟才說,這是以前在用C的概念。

好像應該用 test.c_str() 才對,但是怎麼不行呢?
「漢書文書處理系統」作者,在這向大家學習。
MyBlog 梅僧山房
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Re: wxChar 與 wxString 的關係

Post by samsam598 »

HeMason wrote:
samsam598 wrote:
HeMason wrote: {
 wxChar *p= &text[0] ;
}
这个有点怪怪的。
是怪怪的。
所以小弟才說,這是以前在用C的概念。

好像應該用 test.c_str() 才對,但是怎麼不行呢?
1.wxCStrData wxString::c_str ( ) const

Returns a lightweight intermediate class which is in turn implicitly convertible to both const char* and to const wchar_t*.

2.const wxCharBuffer wxString::mb_str ( const wxMBConv & conv = wxConvLibc ) const

Returns the multibyte (C string) representation of the string using conv's wxMBConv::cWC2MB method and returns wxCharBuffer.
第二个方法默认参数下返回为const char*

显然你至少有两个方案可行:
一是用text.mb_str():

Code: Select all

const char* p=text.mb_str();
printf("%c\n",*++p);// prints 'h'
二是用(const char*)text.c_str():

Code: Select all

const char* p=(const char*)text.c_str();
printf("%c\n",*++p);// prints 'h' also.
请进一步参阅wxString文档下的Conversions functions
一节,另有数种方法均可达到目的,只不过所关注的细节各有不同而已。
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
HeMason
Experienced Solver
Experienced Solver
Posts: 73
Joined: Tue Jun 30, 2009 10:07 am
Location: Taiwan
Contact:

Re: wxChar 與 wxString 的關係

Post by HeMason »

[quote="samsam598"」char* p=text.mb_str(); [/quote]
這提示讓小弟做了另一個嘗試:

wxChar *p=test.wc_str() ;

竟然過關了!

這些轉換真很微細,想信一定很多人像我一樣弄不出個所以然的。

另請教,

const wxChar *p =test.wc_str() ;
wxChar *p =test.wc_str() ;

有沒有 const,使用 *p 時會有差別嗎?


再請教:

是否建議使用 wxString::const_iterator p = text.begin() ;
這用法看起來是能理解,但是和原來 C/C++ 的定義有點格格不入。
什麼時機要用呢?
「漢書文書處理系統」作者,在這向大家學習。
MyBlog 梅僧山房
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Re: wxChar 與 wxString 的關係

Post by samsam598 »

這提示讓小弟做了另一個嘗試:

wxChar *p=test.wc_str() ;

竟然過關了!
==>Windoows 和非WINDOWS下有小小分别。该方法在WINDOWS下才返回宽字符串指针,在非WINDOWS下返回的是wxWCharBuffer临时对象,在编写跨平台软件时要留意。
const wchar_t* wxString::wc_str ( ) const

Converts the strings contents to the wide character represention and returns it as a temporary wxWCharBuffer object (Unix and OS X) or returns a pointer to the internal string contents in wide character mode (Windows).


最不用程序员操心的应该是下面这个:
const wxStringCharType * wx_str () const
Explicit conversion to C string in the internal representation (either wchar_t* or UTF-8-encoded char*, depending on the build).





這些轉換真很微細,想信一定很多人像我一樣弄不出個所以然的。

另請教,

const wxChar *p =test.wc_str() ;
wxChar *p =test.wc_str() ;

有沒有 const,使用 *p 時會有差別嗎?

==》const 定义了该字符串("This is a text")的只读属性,即眼看手莫动(修改),不信你试试:
p[2]='5';
编译一下看看会有什么发生。


再請教:

是否建議使用 wxString::const_iterator p = text.begin() ;
這用法看起來是能理解,但是和原來 C/C++ 的定義有點格格不入。
什麼時機要用呢?

=>这里是把wxString当成STL容器来看,const_iterator 是只读迭代器,容器/迭代器正是C++的重要特性之一,怎么会格格不入呢
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Post by samsam598 »

EDIT:我倒是有了新问题:
http://forums.wxwidgets.org/viewtopic.php?t=26089
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
HeMason
Experienced Solver
Experienced Solver
Posts: 73
Joined: Tue Jun 30, 2009 10:07 am
Location: Taiwan
Contact:

Post by HeMason »

wxChar *p=test.wc_str() ;

==>Windoows 和非WINDOWS下有小小分别。该方法在WINDOWS下才返回宽字符串指针,在非WINDOWS下返回的是wxWCharBuffer临时对象,在编写跨平台软件时要留意。
const wchar_t* wxString::wc_str ( ) const
您的意思是,這用法在Windows有效,但是其他平台的結果會不一樣?
怎麼這樣?為什麼wxWidgets不讓他一樣呢?

您的新問題,我先前也遇到,所以就沒用。
希望有人回。

對於 const 的用法,謝謝您的教導。
小弟受用。
「漢書文書處理系統」作者,在這向大家學習。
MyBlog 梅僧山房
Satervalley
Knows some wx things
Knows some wx things
Posts: 47
Joined: Fri Dec 14, 2007 1:10 am

Post by Satervalley »

HeMason wrote:
Satervalley wrote:一般为了让你的程序无需更改就可在 ansi 或 unicode 模式下转换,需要定义一个通用的char 类型。mfc 下是TCHAR,wxWidgets 下就是wxChar了,一般是一个类型定义,在 ansi 模式下,wxChar 等同于单字节的 char,unicode 模式下,则定义为 w_char,即 16位的宽字符。为了编程方便,还有一个宏 wxT,要记住用。即
不要写 wxString str = "aaaaaa";
而要写 wxString str = wxT("aaaaaa");
感謝!
您說的小弟倒是有在用,舉例時忘了加上。
上面小弟的疑問,不知道您可以回答嗎?
感謝!
问题的原因就是你是用的 unicode 编译的,所以 wxString 的构造函数对你传进来的 ansi 字符串自动进行了转换到 unicode 串,所以你后面单字节访问时会遇到 0 的情况,记住用 wxChar 来访问就好了。
Post Reply