indiocolifa
June 13th, 2003, 03:57 PM
mine is a simple question:
it's recommended to use c++ standard library features when programming Win32 API ??
I mean, for example, using std::string and passing std::string.c_str() to API calls, that require a const char or char*
Or it's better to stick with C types and standard C library ??
Andreas Masur
June 13th, 2003, 04:08 PM
Well...this is basically personal preference. I for example use the STL containers everytime I can thus for your given example I would use the STL string and pass the constant string.
However, there are drawbacks. As you mentioned some of the functions expects a 'char *'. In these cases you need to do some more work like creating a temporary array and copying over since the STL string does not provide direct buffer access (for good reasons).
On the other side, I usually try to be as platform-independent as possible in general (I know that using the API is not much portable)...so I might be driven by that... :cool:
Paul McKenzie
June 13th, 2003, 04:12 PM
Originally posted by indiocolifa
mine is a simple question:
it's recommended to use c++ standard library features when programming Win32 API ??Yes.
I mean, for example, using std::string and passing std::string.c_str() to API calls, that require a const char or char*For const char *, std::string::c_str() is used. For char *, vector<char> is used (in the latter case, you pass a pointer to the first element).
Or it's better to stick with C types and standard C library ?? And lose the safety of string and vector types? Why would you want to do that if you are programming in C++. Which looks easier and safer to you?
// C example
RECT *pRect;
int nRects = whatever;
pRect = (RECT *)malloc( nRects * sizeof(RECT));
MapWindowPoints( hWndSource, hWndDest, pRect, nRects );
pRect = free( pRect );
OR
// C++ example
int nRects = whatever;
std::vector<RECT> pRect( nRects );
MapWindowPoints(hWndSource, hWndDest, &pRect[0], nRects);
In the first example, you need to call malloc correctly. At the end, if you forget to call free(), you have a memory leak. The C++ example has no memory leaks, and is easier to maintain (no calls to malloc or free) .
If you are programming in 'C', then you have no choice but to pick the first one.
Regards,
Paul McKenzie