strange behaviour of wcout used with CStringW

A

Alex Mizrahi

Hello, All!

i admit that it's better to ask questions connected with atl/mfc classes in
special newsgroups, but seems like people there are interested more in
discussing stuff like MFC GUI than C++ 8-/, so i'll better ask here too..

CStringW is a class in atl/mfc dealing with strings 8-]. the only typecast
operator i found is to PCXSTR that is LPCWSTR that is simply "const unsigned
short*" (wchar_t is unsigned short).
wcout accepts wchar_t* strings. so i expect i can ouptut that CStringW into
wcout using <<, or compiler should generate some error.
but it does funny thing - it prints address of string instead of it - << for
const void * is called. even more funny - if i explicitly cast to the
pointer all is well. i tried different types but no one causes printing
address.
such behaviour could be possible if << is defined for CString and it's
broken, but i found no such definition.
i have no ideas what else can be causing this behaviour.. if anybody have
ideas what's happening or how to investigate it - please help.

here's code causing problems:

CStringW myStr(L"A");

std::wcout<<myStr<<std::endl;
std::wcout<<CStringW::pCXSTR(myStr)<<std::endl;
std::wcout<<(const unsigned short*)(myStr)<<std::endl;

generated output is

00325CF0
A
A

i even tried to debug in disassembly - it appears that in all three cases
cast of CStringW to "unsigned short const *" is called, then in first case
it goes into "const void*" output, in other two - into normal string
output..

With best regards, Alex 'killer_storm' Mizrahi.
 
J

Jack Klein

Hello, All!

i admit that it's better to ask questions connected with atl/mfc classes in
special newsgroups, but seems like people there are interested more in
discussing stuff like MFC GUI than C++ 8-/, so i'll better ask here too..

No you better hadn't. People here are interested in discussing stuff
like the C++ language as defined by the ISO standard. ATL and MFC are
strictly off-topic.
CStringW is a class in atl/mfc dealing with strings 8-]. the only typecast

CStringW is completely off-topic here. Unless you are going to
provide the interface and implementation details, kindly go elsewhere.
 
D

David White

Alex Mizrahi said:
Hello, All!

i admit that it's better to ask questions connected with atl/mfc classes in
special newsgroups, but seems like people there are interested more in
discussing stuff like MFC GUI than C++ 8-/, so i'll better ask here too..

CStringW is a class in atl/mfc dealing with strings 8-]. the only typecast
operator i found is to PCXSTR that is LPCWSTR that is simply "const unsigned
short*" (wchar_t is unsigned short).

[snip]

Questions that asssume knowledge of classes and other types that aren't a
part of standard C++ are off-topic. But you can make your question on-topic
by providing the relevant definitions. In this case, the class definition of
CStringW (and anything non-standard it uses that's relevant to the question)
would probably be enough.

DW
 
A

Alex Mizrahi

(message (Hello 'Jack)
(you :wrote :eek:n '(Thu, 09 Dec 2004 23:13:31 -0600))
(
CStringW is a class in atl/mfc dealing with strings 8-]. the only
typecast

JK> CStringW is completely off-topic here. Unless you are going to
JK> provide the interface and implementation details, kindly go
JK> elsewhere.

ok, here is it w/o microsoft classes:

#include <iostream>

typedef const wchar_t* LPCWSTR;

struct mystr_t {
operator LPCWSTR()
{
return L"a";
}
};

void fun(const void* ptr)
{
std::cout<<"const void*"<<std::endl;
}

void fun(const wchar_t * ptr)
{
std::cout<<"const unsigned short*"<<std::endl;
}

int main()
{
mystr_t myStr;

fun (myStr);

std::wcout<<myStr<<std::endl;
std::wcout<<LPCWSTR(myStr)<<std::endl;

}

and if your compiler knows nothing about wcout, this can be any wostream,
where wostream is defined as

typedef basic_ostream<wchar_t, char_traits<wchar_t> > wostream;

output is

const unsigned short*
0041413C
a


there are following << operators defined for ostream:

_Myt& operator<<(const void *_Val)

template<class _Elem, class _Traits> inline
basic_ostream<_Elem, _Traits>& __cdecl operator<<(
basic_ostream<_Elem, _Traits>& _Ostr, const _Elem *_Val)

why does it ignore second one?

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
(prin1 "Jane dates only Lisp programmers"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top