Click here to Skip to main content
15,914,409 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Question of style Pin
Navin12-Jun-02 10:06
Navin12-Jun-02 10:06 
GeneralRe: Question of style Pin
Chris Losinger12-Jun-02 10:04
professionalChris Losinger12-Jun-02 10:04 
GeneralRe: Question of style Pin
Shog912-Jun-02 13:21
sitebuilderShog912-Jun-02 13:21 
GeneralRe: Question of style Pin
Chris Losinger12-Jun-02 13:26
professionalChris Losinger12-Jun-02 13:26 
GeneralRe: Question of style Pin
Shog912-Jun-02 13:28
sitebuilderShog912-Jun-02 13:28 
GeneralRe: Question of style Pin
redeemer12-Jun-02 10:23
redeemer12-Jun-02 10:23 
GeneralRe: Question of style Pin
Navin12-Jun-02 15:41
Navin12-Jun-02 15:41 
GeneralRe: Ok, here is the run down... Pin
Tim Smith12-Jun-02 16:11
Tim Smith12-Jun-02 16:11 
You have to look at what you are doing with your string. What is coming in and how it will be used inside the routine.

Outside routine: CString
Inside routine: CString (for example, storing into a local str var)
Argument: const CString &

In this case, you are able to take advantage of the reference counting used by CString. No duplicate strings will be created. (GOOD)

Outside routine: CString
Inside routine: CString (for example, storing into a local str var)
Argument: LPCTSTR

In this case, the CString will just pass the pointer to the internal string. However, once that string is used as a CString inside of the routine, a duplicate will have to be made. (BAD)

Outside routine: LPCTSTR
Inside routine: CString (for example, storing into a local str var)
Argument: const CString &

In this case, a CString will have to be created prior to the routine being invoked. However, since the routine will actually be using the CString internally, it would have had to have been created anyway. No performance loss. (GOOD)

Outside routine: LPCTSTR
Inside routine: CString (for example, storing into a local str var)
Argument: LPCTSTR

In this case, the string is being passed directly into the routine. Then internally a CString is being created. However, the same would have been true if the argument was CString. (GOOD)

Outside routine: CString
Inside routine: LPCTSTR (maybe a call to _tcscmp)
Argument: const CString &

In this case, the string is passed directly in and the routine just uses the (LPCTSTR) operator in CString to access the contents. No performance loss. (GOOD)

Outside routine: CString
Inside routine: LPCTSTR (maybe a call to _tcscmp)
Argument: LPCTSTR

In this case, the CString will just pass the pointer to the internal string. The routine will use the pointer as is. (GOOD)

Outside routine: LPCTSTR
Inside routine: LPCTSTR (maybe a call to _tcscmp)
Argument: const CString &

In this case, a copy of the string will get created to be passed as a CString. Then the CString is just dereferneced. Needless creation of temp string. (BAD)

Outside routine: LPCTSTR
Inside routine: LPCTSTR (maybe a call to _tcscmp)
Argument: LPCTSTR

In this case, the string is being used as is. No temp objects. (GOOD)


SOOOOOOO

If you total it all up:

Case 1:

If inside the routine you are using the string as a CString:

Argument as CString: 2 good cases, no bad.
Argument as LPCTSTR: 1 good case, 1 bad case.

So, when the routine will be using the string as a CString, there is no reason to not use "const CString &" for the argument.

Case 2:

If inside the routine you are using the string as a LPCTSTR:

Argument as CString: 1 good case, 1 bad case.
Arguemnt as LPCTSTR: 2 good cases, no bad cases.

So, when the routine will be using the string as a LPCTSTR, there is no reason to not use "LPCTSTR".













Tim Smith

I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
Generalhelp Pin
12-Jun-02 8:09
suss12-Jun-02 8:09 
GeneralRe: help Pin
Michael P Butler12-Jun-02 11:16
Michael P Butler12-Jun-02 11:16 
GeneralReadFile from Comm port Pin
JohnnyG12-Jun-02 7:19
JohnnyG12-Jun-02 7:19 
GeneralRe: ReadFile from Comm port Pin
JohnnyG12-Jun-02 10:16
JohnnyG12-Jun-02 10:16 
GeneralWindows Messaging Timing and Threads Pin
SanShou12-Jun-02 6:41
SanShou12-Jun-02 6:41 
GeneralRe: Windows Messaging Timing and Threads Pin
NormDroid12-Jun-02 9:40
professionalNormDroid12-Jun-02 9:40 
GeneralCallbakc Functions Pin
arthivjii12-Jun-02 6:36
arthivjii12-Jun-02 6:36 
GeneralRe: Callbakc Functions Pin
Chris Losinger12-Jun-02 6:55
professionalChris Losinger12-Jun-02 6:55 
GeneralRe: Callbakc Functions Pin
Alexandru Savescu12-Jun-02 8:43
Alexandru Savescu12-Jun-02 8:43 
GeneralRe: Callbakc Functions Pin
Chris Losinger12-Jun-02 9:00
professionalChris Losinger12-Jun-02 9:00 
GeneralInstallshield questions... Pin
Mike Zinni12-Jun-02 5:36
Mike Zinni12-Jun-02 5:36 
GeneralRe: Installshield questions... Pin
Navin12-Jun-02 8:33
Navin12-Jun-02 8:33 
QuestionHi! everybody! How about "graph line printing"? Pin
12-Jun-02 5:41
suss12-Jun-02 5:41 
QuestionHow to fill a vector or a list with data from a file! Pin
JMajors9812-Jun-02 4:50
JMajors9812-Jun-02 4:50 
AnswerRe: How to fill a vector or a list with data from a file! Pin
Chris Losinger12-Jun-02 5:39
professionalChris Losinger12-Jun-02 5:39 
AnswerRe: How to fill a vector or a list with data from a file! Pin
Joaquín M López Muñoz12-Jun-02 11:57
Joaquín M López Muñoz12-Jun-02 11:57 
AnswerRe: How to fill a vector or a list with data from a file! Pin
Mike Nordell13-Jun-02 3:23
Mike Nordell13-Jun-02 3:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.