Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - CString function parameter
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

CString function parameter

 Post Reply Post Reply
Author
Message
jimmy View Drop Down
Senior Member
Senior Member


Joined: 11 November 2003
Location: Austria
Status: Offline
Points: 516
Post Options Post Options   Thanks (0) Thanks(0)   Quote jimmy Quote  Post ReplyReply Direct Link To This Post Topic: CString function parameter
    Posted: 23 January 2006 at 4:30am
Hello CodeJock Team,

1) the codejock software is great.

2) optimize point
    a big part of the software have FunctionName(CString strParam)
but it is better use FunctionName(const CString& strParam)
The software ist much faster, less memory defragmentation, smaller code size.
Please, change this for the next release.
also look for other function parameter, to add the parameter as
const referenz.

    Jimmy

Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 24 January 2006 at 1:56pm

Hi,

1. Agree :)  Thank you

2. Actually in 9.81 we changed most of "CString strParameter" to "LPCTSTR lpszParameter"

some virtual methods we can't change to avoid your complains about compatibility ;)

 

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
jimmy View Drop Down
Senior Member
Senior Member


Joined: 11 November 2003
Location: Austria
Status: Offline
Points: 516
Post Options Post Options   Thanks (0) Thanks(0)   Quote jimmy Quote  Post ReplyReply Direct Link To This Post Posted: 25 January 2006 at 5:09am
Hi oleg,

LPCTSTR is also not good, because
if you do m_strCaption = lpszParameter it always make
a strlen, alloc.
It's slow, and higher memory defragmentation.
For compatibility it's ok.
But for new, i think const CString& is the best solution.

    Jimmy


Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 25 January 2006 at 8:49am

lets see.

if method declared as SomeMethods(cont CString& strItem)

and called as

SomeMethod(_T("SomParameter"));

anyway compiller first create CString variable and assign it with "SomParameter", and later destroy it.

 

 

Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
Warren View Drop Down
Groupie
Groupie
Avatar

Joined: 23 February 2005
Status: Offline
Points: 64
Post Options Post Options   Thanks (0) Thanks(0)   Quote Warren Quote  Post ReplyReply Direct Link To This Post Posted: 07 February 2006 at 10:30am

Jimmy,

You said "...LPCTSTR is also not good..." but according to Microsoft: "...in most cases it is best to declare the string function parameter as LPCTSTR..."

http://msdn.microsoft.com/library/default.asp?url=/library/e n-us/vccore/html/_core_Strings.3a_.CString_Argument_Passing. asp

Back to Top
jimmy View Drop Down
Senior Member
Senior Member


Joined: 11 November 2003
Location: Austria
Status: Offline
Points: 516
Post Options Post Options   Thanks (0) Thanks(0)   Quote jimmy Quote  Post ReplyReply Direct Link To This Post Posted: 12 February 2006 at 8:21am
Hi,

1) if i call a function many time, i use

static CString s_strText(_T("My Text"));

Function( s_strText );
And void Function( const CString& strText );

So it must alloc the text only one time.

In some function i use also (LPCTSTR lpText), but only if it call not often.

also use as return parameter
const CString& GetName()
{ return m_stName; }

The most time if you call GetName (or other) is, to put the paramter to other function.

x.SetName( y.GetName() );

So this is much faster than always create a new CString.

If i look into our program code, 95 percent of function call put a CString as parameter.

    Jimmy

Back to Top
Warren View Drop Down
Groupie
Groupie
Avatar

Joined: 23 February 2005
Status: Offline
Points: 64
Post Options Post Options   Thanks (0) Thanks(0)   Quote Warren Quote  Post ReplyReply Direct Link To This Post Posted: 12 February 2006 at 4:37pm
Are you under the impression that CString allocates a new copy of the text, and copies the text into the new buffer each time you return "CString" instead of "const CString&"? Because that is not how it works.

AFAIK, CString has a COW (copy on write) implementation, so what are you really saving? It's basically:

return 1 pointer/reference
vs.
change reference count, return 1 pointer/reference

Either way, I wouldn't worry about it nowadays. With 2+ Ghz computers, 99% of optimization is algorithmic and not tweaking code.

That's just my 2 cents...
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.031 seconds.