GBrookman
GBrookman

Reputation: 433

CString assignment error

Can someone explain why strY assignment won't compile? I thought that the compiler might have replaced the assignment with a constructor but strZ compiles, as does strX.

unsigned char szArr[]{ 0xD7, 0x53, 0xBF, 0xE7};

CString strX;
strX = szArr;
CString strY = szArr;
// no suitable constructor exists to convert unsigned char[4] to ...
CString strZ(szArr);

Upvotes: 3

Views: 919

Answers (1)

GBrookman
GBrookman

Reputation: 433

After some further reading/playing it seems strZ is using this constructor:

CSTRING_EXPLICIT CStringT(_In_z_ const unsigned char* pszSrc) :
    CThisSimpleString( StringTraits::GetDefaultManager() ) 

And that works because it's explicitly called

strX is using the default constructor then assignment operator:

CStringT& operator=(_In_opt_z_ const unsigned char* pszSrc)

And thanks to ChristopherOicles comment:

When _ATL_CSTRING_EXPLICIT_CONSTRUCTORS is defined, all CString constructors that take a single parameter are compiled with the explicit keyword, which prevents implicit conversions of input arguments.

So without _ATL_CSTRING_EXPLICIT_CONSTRUCTORS defined strY will use the same constructor as strZ. The stub for a Windows console app has this defined in stdafx.h

Upvotes: 2

Related Questions