This might sounds simple but I want to know if one way is actually more efficient.
Example one:
CString strName = _T("");
CString strName = CString();
Example two:
if (strFilterText == _T(""))
if (strFilterText.IsEmpty())
Is there any negative impact is using _T("")
in this way? And if so, why?
CodePudding user response:
CString
has different constructors and operator overloads, it is prepared for different scenarios. It's a complicated class but in general, such classes work like this:
Use default constructor CString()
(initializes for _T("")
):
CString s1 = CString();
CString s2; //same as above, shortcut
CString s3{}; //same as above
CString s4 = {}; //same as above
Use copy constructor CString(const CString& src)
:
const CString src = _T("");
CString s1 = CString(src);
CString s2 = src; //same as above
CString s3 = { src}; //same as above
Use different constructor CString(const TCHAR* src)
:
const TCHAR* src = _T("");
CString s1 = CString(src);
CString s2 = src; //same as above
CString s3 = { src }; //same as above
CString
also supports =
assignment operator:
CString s;
s = _T(""); //uses `=` assignment operator
s = CString(); //uses default constructor for right side,
//calls `=` assignment operator for left side.
==
operator overload may use CString::Compare
to compare the two sides. So
if (s == _T(""))
is same as if (s.Compare(_T("")) == 0)
, it might be 1 nanosecond slower than s.IsEmpty()
, otherwise is valid.