Re: passing a string to a dll
On Wed, 19 Sep 2007 19:41:39 +0200, "Giovanni Dicanio"
<giovanni.dicanio@invalid.it> wrote:
But CString really has got both a LPCTSTR conversion operator and a LPCTSTR
constructor...
So, is CString bad designed?
For the era in which it was designed, no, but it's been recognized for
years that implicit conversions are usually best avoided. String classes
are a little different than most other classes, though, since strings are
used so frequently. Most classes should make their single-argument
constructors "explicit", but for a string class, it's pretty desirable for
the char* -> string conversion to be automatic, so the ctor is fine.
And is this the reason why std::string has const char * constructor, but no
implicit conversion operator (we are forced to explicitly call .c_str()
method) ?
The "operator char*" is definitely considered more evil than the ctor. It
allows class state to escape pretty easily, it can cause ambiguity with
array access, etc. Then again, when programming for Windows, it's
convenient to have the conversion happen automatically, so the trade-off is
reasonable. That said, I don't mind writing c_str(), but I do wish VC would
warn about passing "s" to the ellipsis, where s is a std::string. I entered
feedback on this a couple of years ago:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98899
--
Doug Harrison
Visual C++ MVP