|
From: David Webber on 4 May 2008 19:32 "Jeov� Almeida" <jeovaalmeida(a)yahoo.com> wrote in message news:%2305F%23qirIHA.5872(a)TK2MSFTNGP04.phx.gbl... > How can I make a CString parameter optional? > > I tried > > [in the .h file:] > virtual CString Get(CString strURL, CString& strErrorMsg = NULL); > > It gives me the error > Error 4 error C2440: 'default argument' : cannot convert from 'int' to > 'CString & > > What I want to do: make the strErrorMsg parameter optional so I could call > CString result = obj.Get("www.server.com"); > CString errMsg; > CString result2 = obj.Get("www.server.com", errMsg); One obvious way is to overload the function: CString Get( const CString &strURL ); CString Get( const CString &strURL, CString &errMsg ); Dave -- David Webber Author of 'Mozart the Music Processor' http://www.mozart.co.uk For discussion/support see http://www.mozart.co.uk/mozartists/mailinglist.htm
From: Jeov� Almeida on 4 May 2008 19:46 Feedback: Analysing all the sugestions, and considering strErr is an output parameter: virtual CString Get(LPCTSTR strURL, CString& strErrorMsg=CString() ); which solved my problem. Thanks all for the tips and lessons. Jeov� "Jeov� Almeida" <jeovaalmeida(a)yahoo.com> escreveu na mensagem news:%2305F%23qirIHA.5872(a)TK2MSFTNGP04.phx.gbl... > Hello, > > How can I make a CString parameter optional? > > I tried > > [in the .h file:] > virtual CString Get(CString strURL, CString& strErrorMsg = NULL); > > It gives me the error > Error 4 error C2440: 'default argument' : cannot convert from 'int' to > 'CString & > > What I want to do: make the strErrorMsg parameter optional so I could call > CString result = obj.Get("www.server.com"); > CString errMsg; > CString result2 = obj.Get("www.server.com", errMsg); > >
From: Joseph M. Newcomer on 5 May 2008 02:26 Well, the message is correct; you can't default a reference parameter. virtual CString Get(const CString & strURL, CString * strErrorMsg = NULL) or just do virtual CString Get(const CString & strULR, CString & strErrorMsg) and then have an overload virtual CString Get(const CString & strULR) { CString dummy; return Get(strULR, dummy); } joe On Sun, 4 May 2008 17:06:35 -0400, "Jeov� Almeida" <jeovaalmeida(a)yahoo.com> wrote: >Hello, > >How can I make a CString parameter optional? > >I tried > >[in the .h file:] >virtual CString Get(CString strURL, CString& strErrorMsg = NULL); > >It gives me the error >Error 4 error C2440: 'default argument' : cannot convert from 'int' to >'CString & > >What I want to do: make the strErrorMsg parameter optional so I could call >CString result = obj.Get("www.server.com"); >CString errMsg; >CString result2 = obj.Get("www.server.com", errMsg); > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Serge Wautier on 5 May 2008 02:30 > I don't want to be picky Neither do I ;-) > I also tend to pass LPCTSTR as input string parameters Why lose the benefits of CString? In addition, if your function contains CString myString(pszInputStr); -> duplicates the string buffer. Expensive & useless! My 2 cents, Serge. http://www.apptranslator.com - Localization tool for your MFC applications "Giovanni Dicanio" <giovanni.dicanio(a)invalid.com> wrote in message news:OuPQUBjrIHA.3780(a)TK2MSFTNGP03.phx.gbl... > > "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> ha scritto nel > messaggio news:ewoK9zirIHA.4884(a)TK2MSFTNGP06.phx.gbl... > >> virtual CString Get(CString strURL, LPCTSTR strErrorMsg = NULL); > > I don't want to be picky, but I'd like to add to correct Scott's answer > that you should aksi consider passing CString input parameters by *const > reference* (so no copy constructor is called): > > // Note const CString & > CString Get( /* input */ const CString & strURL ... ) > > I also tend to pass LPCTSTR as input string parameters, and use CString > for output parameters or return values. > > However, I read that the OP's method is virtual, so I'm not sure if using > default parameters is good for virtual methods... > > Maybe for virtual methods, using overload is better than using default > parameters (at least when there are few default parameters), e.g. > > // Two overloads for virtual methods (to "simulate" default parameters) > virtual CString Get( const CString & strURL, const CString & > strErrorMsg ); > virtual CString Get( const CString & strURL ); // default strErrorMsg > > Giovanni > >
From: Giovanni Dicanio on 5 May 2008 03:37 "Serge Wautier" <serge(a)wautier.nospam.net> ha scritto nel messaggio news:eTyYdnnrIHA.5872(a)TK2MSFTNGP04.phx.gbl... >> I don't want to be picky > > Neither do I ;-) :-) >> I also tend to pass LPCTSTR as input string parameters > > Why lose the benefits of CString? Doug answered this point in his detailed post. In his words: <cite> Finally, if the parameters don't /need/ to be CStrings, use LPCTSTR parameters. That way, when you say... CString result = obj.Get("www.server.com"); .... you don't needlessly construct a temporary CString to represent the parameter. </cite> I used to pass 'const CString &' references for input strings, but then after reading some source code (maybe it was ATL Server code... or some article on MSDN, I'm not sure), I noted that technique of using LPCTSTR (or explicit 'const wchar_t *') just for input string parameters (and CString for output parameters or return values). Then I realized that that would save the creation of a temporary CString instance in cases like shown in Doug's post. It is one of the good coding practices, like *pre*incrementing an STL iterator (++i instead of i++), that does not cost much to us (the programmers), and contributes to build more quality code, IMHO. Giovanni
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How to maximize an MDI child window ? Next: Trap MaxText trigger |