CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Location
    Florida
    Posts
    2

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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

  3. #3
    Join Date
    Dec 2009
    Posts
    145

    Re: Which is more appropriate in C++ code?

    Quote Originally Posted by DangerD View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured