-
September 24th, 2010, 10:19 PM
#1
Which is more appropriate in C++ code?
As I learn C++ I try to avoid bad programming practices and teach myself good habits. I stick very strictly to C++ and the Standard Library and would like an opinion on something.
Example below:
Code:
vector<TCHAR> Path(MAX_PATH);
if(!GetModuleFileName(hModule, &Path[0], Path.size()))
{
wcerr << _T("Error: Couldn't get file path.") << endl;
return false;
}
m_FilePath = &Path[0];
Is it better practice to use a char array in c++ code instead of a vector in a situation like this?
Thanks, DangerD.
-
September 24th, 2010, 11:35 PM
#2
Re: Which is more appropriate in C++ code?
Since MAX_PATH is a constant, then all you need is a char array. If you didn't know up front the number of characters, then a vector that is then resized would be appropriate.
Regards,
Paul McKenzie
-
September 25th, 2010, 02:26 AM
#3
Re: Which is more appropriate in C++ code?
Originally Posted by DangerD
As I learn C++ I try to avoid bad programming practices and teach myself good habits. I stick very strictly to C++ and the Standard Library and would like an opinion on something.
Example below:
Code:
vector<TCHAR> Path(MAX_PATH);
if(!GetModuleFileName(hModule, &Path[0], Path.size()))
{
wcerr << _T("Error: Couldn't get file path.") << endl;
return false;
}
m_FilePath = &Path[0];
Is it better practice to use a char array in c++ code instead of a vector in a situation like this?
Thanks, DangerD.
Thank you DangerD, that is one among many cases where old array I think is more preferable. So
TCHAR path[MAX_PATH];
is there for you
http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|