If I have a #define GAMENAME "POSEIDON" and would like to cast it over to either a LPCSTR or std::string how would I go about doing it ‘properly’?
Fx:
m_hwnd = CreateWindowEx(NULL,
"GLClass",
/* My GAMENAME #define goes here */
(LPCSTR)GAMENAME,
dwStyle | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
/* The X,Y coordinate */
0, 0,
m_windowRect.right - m_windowRect.left,
m_windowRect.bottom - m_windowRect.top,
/* TODO: Handle to Parent */
NULL,
/* TODO: Handle to Menu */
NULL,
m_hinstance,
this);
Perhaps I am just taking a bad approach to this?
#defines are processed by the preprocessor so GAMENAME is not really a variable in your program. Use of casts as demonstrated the answer to your own question would seem to make the problem worse. Mike Dunn’s second article on strings at Code Project explains why casts are not the best option. The first article is also worth reading obviously.
You should be able to create a std::string like this:
Then create the LPCSTR from it:
In your program you would pass psz to CreateWindowEx(). Another thing worth thinking about is whether the game name really needs to change? If not it’s better making this a constant.