1

Need to log the content of buf using the LogMethod() below the problem is that LogMethos only accepts a "Const CString&"

char buf[1024];
strcpy(buf, cErrorMsg);

// need to pass to LogMethod "buf" how do i do that?
log.LogMethod(const CString &); 

Thans Rev

Reversed

Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
Reversed
  • 51
  • 1
  • 10

3 Answers3

1

If you're talking about MFC CString, as far as I can tell, it should have a non-explicit constructor taking TCHAR const *. In other words, the following should work.

log.LogMethod(buf); 

If it doesn't, please post the error message.

avakar
  • 32,009
  • 9
  • 68
  • 103
1
log.LogMethod(CString(buf));

This will avoid the problem where the compiler won't automatically create the CString object using the appropriate constructor since the argument is a reference (It would have if the argument was a "plain" CString).

Ruddy
  • 1,734
  • 10
  • 11
  • It makes no difference whether the parameter is `CString` or `CString const &`, in both cases the conversion will be performed. In the latter case, a temporary will be created and bound to the reference. – avakar Jan 14 '10 at 16:35
  • That is true in the general conversion case, however for CString this isn't true because the constructor (that accepts a char*) has been declared explicit which, unless i'm mistaken, keeps the compiler from being able to automatically chose it for such conversions. That said it does work in VS6 (where it is not marked explicit) when the CString& is const, but it does not work in VS2005 Reason: cannot convert from 'const char [5]' to 'const CString' Constructor for class 'ATL::CStringT' is declared 'explicit' – Ruddy Jan 14 '10 at 17:04
  • I very much doubt that Microsoft would change the explicitness between versions, as it would break a lot of existing code. Besides, documentation (http://msdn.microsoft.com/en-us/library/cws1zdt8%28VS.80%29.aspx) clearly shows the constructor is not explicit. Are you sure you didn't try compiling as Unicode while still passing `char const *` as a parameter (which indeed would trigger the explicit non-TCHAR constructor)? – avakar Jan 14 '10 at 21:46
  • @avakar - I stand corrected. I had accepted the defaults for the project, it was making "const char*" unicode, and when changed to non-unicode it worked as you describe (compiling without error). – Ruddy Jan 14 '10 at 23:34
0
CString cs;
cs = buf;

log.LogMethod(cs)
sdornan
  • 2,805
  • 2
  • 15
  • 7