Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Given that
TCHAR szString[] = _T("Sample");
CString stString;


which of the following is better
1. stString += szString; // Edited to fix type (orignal: sString)
2. stString += (LPCTSTR)szString;

Summary of question:
When concatenating a Cstring with a null terminated string is it advisable to cast to (LPCTSTR) to elimate warning?
Posted
Updated 23-Apr-14 8:13am
v6
Comments
Gbenbam 23-Apr-14 14:39pm    
Ok.I figured it out on my own.It doesn't matter whether we are concatenating withva LPCTSTR or LPTSTR.In both case the += operator recieves the right operand as a LPCTSTR into its parameter.So there is absolutely no difference between concatenating with a LPCTSTR and a LPTSTR.

Solution discovered.Case closed.

1 solution

The question makes no sense at all. If two lines do completely different things, how can you ask which one is better?
Besides, if you already use CString, why using LPTSTR at all (well, maybe only for compatibility with some existing code). Also, did you consider using standards strings std::string?

—SA
 
Share this answer
 
Comments
Gbenbam 23-Apr-14 14:26pm    
I guese the problem is that I don't really fully understand how the plus operator works with CString.
You are right the question made no sense but I have made appropriate cmidifucation.(Changed LPTSTR to LPCTSTR).
The examples in msdn uses LPCTSTR, but I have a DLL that returns LPTSTR.

See the following MSDN page

http://msdn.microsoft.com/en-us/library/aa300548(v=vs.60).aspx
Sergey Alexandrovich Kryukov 23-Apr-14 15:32pm    
The difference is "const". First, you need to understand that CString is class, and other types are just pointers.
The operator you mentioned is not "plus", this is "+=", a very different thing, it returns the reference to the same string after concatenation of the right part. Why not reading original MSDN documentation? Please see:
http://msdn.microsoft.com/en-us/library/aa300548%28v=vs.60%29.aspx

Are you going to accept the answer formally (green "Accept" button)?

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900