ATL CString
-
Hello All, here I have another 2 doubt of ATL::CString 1. I am not able to overridden ATL::CString class, in a proper way. Is it necessary to override all the methods of this class? I want to override only a few actually. 2. for all the character operation, it warns me " conversion from 'wchar_t' to 'char', possible loss of data"
char tt = strTemp[0];
Is there any way to convert 'wchar_t' to 'char' as in global way, so that i don't need to typecast it everytime like
char tt = (char)strTemp[0];
thanks in advance hrishi
-
Hello All, here I have another 2 doubt of ATL::CString 1. I am not able to overridden ATL::CString class, in a proper way. Is it necessary to override all the methods of this class? I want to override only a few actually. 2. for all the character operation, it warns me " conversion from 'wchar_t' to 'char', possible loss of data"
char tt = strTemp[0];
Is there any way to convert 'wchar_t' to 'char' as in global way, so that i don't need to typecast it everytime like
char tt = (char)strTemp[0];
thanks in advance hrishi
I guess your project setting in UNICODE. If so, CString will be using wide char (wchar_t) to manipulate strings. The problem is, you are treating them as multibyte characters (char). I suggest you to use TCHAR type instead of hard coding char & wchar_t, as it can be used well in both unicode and multibyte configurations.
-
I guess your project setting in UNICODE. If so, CString will be using wide char (wchar_t) to manipulate strings. The problem is, you are treating them as multibyte characters (char). I suggest you to use TCHAR type instead of hard coding char & wchar_t, as it can be used well in both unicode and multibyte configurations.
Thanks for your reply :) what about setting the project as character set : Not set, in this case, as your suggestion, do I need to replaced all char to TCHAR in the whole project. Thats a lot :) . . I was trying to avoid much touch in the project implementation :) How about having my own class CMyString : public ATL::CString, can i do something in this way to avoid less touch to the project code ? Please note that the current project uses char not wchar_t. Project setting could be change according to our convenience thanks in advance hrishi
-
Thanks for your reply :) what about setting the project as character set : Not set, in this case, as your suggestion, do I need to replaced all char to TCHAR in the whole project. Thats a lot :) . . I was trying to avoid much touch in the project implementation :) How about having my own class CMyString : public ATL::CString, can i do something in this way to avoid less touch to the project code ? Please note that the current project uses char not wchar_t. Project setting could be change according to our convenience thanks in advance hrishi
1. If character set is 'Not Set', i think char will work. 2. CMyString can be alright. Write it to take char only, and internally convert them to wchar_t if the Character Set is 'Unicode' (using APIs like MultiByteToWideChar) and let the CString base class to do the string operations. This will help you to retain all the char types in existing code, provided you are replacing all usage of CString with CMyString. And the implied issue is that you won't be able to manipulate non-english characters with CMyString.
-
Hello All, here I have another 2 doubt of ATL::CString 1. I am not able to overridden ATL::CString class, in a proper way. Is it necessary to override all the methods of this class? I want to override only a few actually. 2. for all the character operation, it warns me " conversion from 'wchar_t' to 'char', possible loss of data"
char tt = strTemp[0];
Is there any way to convert 'wchar_t' to 'char' as in global way, so that i don't need to typecast it everytime like
char tt = (char)strTemp[0];
thanks in advance hrishi
You cannot use casting to convert from a large element type to a smaller one without potential loss of data, you must use some form of conversion. If you stick with the default
CString
[^] class it will handle eitherchar
orwchar_t
depending on your project settings. If your project needs to use both Unicode and MBCS then you need to provide conversion functions at the appropriate parts of your application. If it will only ever use one or the other but you need to provide a version that will handle each type then useTCHAR
as the type for all your character variables and_T("constant string")
macros for all constants. That way you can simply rebuild your project to handle the appropriate encoding.The best things in life are not things.
-
1. If character set is 'Not Set', i think char will work. 2. CMyString can be alright. Write it to take char only, and internally convert them to wchar_t if the Character Set is 'Unicode' (using APIs like MultiByteToWideChar) and let the CString base class to do the string operations. This will help you to retain all the char types in existing code, provided you are replacing all usage of CString with CMyString. And the implied issue is that you won't be able to manipulate non-english characters with CMyString.
Thanks for your reply, It was of great help. I followed the same way as you suggested. But found a very interesting problem. Following is the explanation: I should be able to use + operator like below..
CMyString strTemp;
strTemp = "aa" + strTemp;And for this I need to overload the +operator.(Reason:: I get a error which says::
error C2678: binary '+' : no operator found which takes a left-hand operand of type 'const wchar_t [3]'
I am not able to find a appropriate way to call the operator+ of the base class, or any other way to solve the same. [ All I need is, inside operator+ overloading , to convert the char to wchar and then pass it to the base class] Please help me. thanks in advance Hrishi
-
You cannot use casting to convert from a large element type to a smaller one without potential loss of data, you must use some form of conversion. If you stick with the default
CString
[^] class it will handle eitherchar
orwchar_t
depending on your project settings. If your project needs to use both Unicode and MBCS then you need to provide conversion functions at the appropriate parts of your application. If it will only ever use one or the other but you need to provide a version that will handle each type then useTCHAR
as the type for all your character variables and_T("constant string")
macros for all constants. That way you can simply rebuild your project to handle the appropriate encoding.The best things in life are not things.
-
Thanks for your reply, It was of great help. I followed the same way as you suggested. But found a very interesting problem. Following is the explanation: I should be able to use + operator like below..
CMyString strTemp;
strTemp = "aa" + strTemp;And for this I need to overload the +operator.(Reason:: I get a error which says::
error C2678: binary '+' : no operator found which takes a left-hand operand of type 'const wchar_t [3]'
I am not able to find a appropriate way to call the operator+ of the base class, or any other way to solve the same. [ All I need is, inside operator+ overloading , to convert the char to wchar and then pass it to the base class] Please help me. thanks in advance Hrishi
-
Thanks for your reply, It was of great help. I followed the same way as you suggested. But found a very interesting problem. Following is the explanation: I should be able to use + operator like below..
CMyString strTemp;
strTemp = "aa" + strTemp;And for this I need to overload the +operator.(Reason:: I get a error which says::
error C2678: binary '+' : no operator found which takes a left-hand operand of type 'const wchar_t [3]'
I am not able to find a appropriate way to call the operator+ of the base class, or any other way to solve the same. [ All I need is, inside operator+ overloading , to convert the char to wchar and then pass it to the base class] Please help me. thanks in advance Hrishi
I think you need to bite the bullet and convert all your char variables and constants to TCHAR. If you do not, you will continue to have problems like this. If it were easy to solve the multi-byte to Unicode to multi-byte character conversion issues that solution would be part of the CString class. The changes you need to make are mechanical. You need to put _T() around all your string and character literals. You need to change all your char variables to TCHAR. I don't know how big your project is, or if you have access to all the source. I suspect you have spent more time trying to avoid the issue than a proper fix would require.