我非常了解将CString
转换为C风格字符的技巧。其中之一是使用strcpy
/_tcscpy
,其他包括使用CStrBuf
。从CString转换为char */TCHAR *
问题:
char Destination[100];
CStringA Source; // A is for simplicity and explicit ANSI specification.
Source = "This is source string."
现在我想这样的:
Destination = Source;
自动发生。那么,这在逻辑上意味着在CString
类中写入一个转换运算符。但是,就像它是隐含的,我没有权限来更改CString
类。
我想写一个全局转换操作符和全局赋值操作符。但它不工作:
operator char* (const CStringA&); // Error - At least must be class-type
operator = ... // Won't work either - cannot be global.
是的,这是绝对有可能写的函数(最好是一个模板)。但是这涉及到的调用的功能,并且它不是流畅的赋值运算符。
我不知道,但你为什么不能定义一个全局'void运算符=(字符*&LHS,常量的CString&右)'?其中一个参数不是内置类型,所以重载应该是可能的。 –
@Kerrek:赋值必须作为非静态成员函数重载。 –
你不能在CString中使用GetBuffer(..)吗? – Simon