How to prevent C6284 when using CString :: Format?

The following code generates warning C6284 when compiling with /analyze in MSVC 2008: an object passed as parameter '% s' when a string is required in a function call.

  CString strTmp, str; str = L"aaa.txt" strTmp.Format (L"File: %s", str); 

I am looking for a good solution for this that does not require static_cast

+6
compiler-warnings visual-c ++
source share
2 answers

Microsoft describes the use of CString with variable argument functions here :

 CString kindOfFruit = "bananas"; int howmany = 25; printf_s( "You have %d %s\n", howmany, (LPCTSTR)kindOfFruit ); 

Alternatively, you can also use the PCXSTR CString::GetString() const; method PCXSTR CString::GetString() const; to try to fix the warning:

 CString strTmp, str; str = L"aaa.txt" strTmp.Format (L"File: %s", str.GetString()); 
+6
source share

One of the design flaws of Creering, err, features is that it has an implicit conversion to LPCTSTR , which makes the warning LPCTSTR IMHO. But anyway, if you look at the Microsoft documentation , they actually use casts in their own example. I really don't see the need to avoid static_cast here, in fact I would welcome it, as it makes the explicit conversion more explicit and therefore easier to define.

+3
source share

All Articles