Take the 2-minute tour ×

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


share|improve this question
what is CString? do you refer to ATL/MFC? In C++, the standard string is std::string. CString can be anything, for instance a class coded by a coworker in your code base. –  Gregory Pakosz Jan 14 '10 at 15:46
//log.LogDebug(CString(buf)); //log.LogDebug(buf); //str = buf; //log.LogDebug(str); Neither of the code above worked... –  Reversed Jan 14 '10 at 18:27
Then the problem is somewhere else. Show us the code for LogMethod. –  avakar Jan 14 '10 at 21:47
CString i use is from MFC class i´m using VS6 –  Reversed Jan 15 '10 at 12:11
You were right! the problem was in my LogMethod class Thanks all for your help –  Reversed Jan 15 '10 at 12:46
add comment

3 Answers

up vote 1 down vote accepted

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.


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

share|improve this answer
no error.....just doesnt log –  Reversed Jan 14 '10 at 18:29
add comment

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).

share|improve this answer
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<BaseType,StringTraits>' 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 (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
add comment
CString cs;
cs = buf;

share|improve this answer
add comment

Your Answer


