|
From: Jeov� Almeida on 4 May 2008 17:06 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: Scott McPhillips [MVP] on 4 May 2008 17:22 You can't NULL a reference. Assuming that the strErrorMsg is only used for input to the function, a clean way to do this would be to make the function declaration like this: virtual CString Get(CString strURL, LPCTSTR strErrorMsg = NULL); This will permit callers to omit the 2nd parameter, or to pass a CString as the 2nd parameter. "Jeov� Almeida" <jeovaalmeida(a)yahoo.com> wrote in message 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); > > -- Scott McPhillips [VC++ MVP]
From: Giovanni Dicanio on 4 May 2008 17:45 "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: Doug Harrison [MVP] on 4 May 2008 18:37 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 & Right, there is no such thing as a null reference in C++. If you want the null concept, you'll have to use pointers. >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); Though not quite the same as NULL, you could use: virtual CString Get( CString strURL, // If you don't modify, use a const reference. const CString& strErrorMsg = CString()); But I wouldn't use default parameters with a virtual function. A better approach would be: CString Get( CString strURL, const CString& strErrorMsg = CString()) { return Get_impl(strURL, strErrorMsg); } virtual CString Get_impl( const CString& strURL, const CString& strErrorMsg); The other reason to use this technique is to avoid overloading virtual functions, which reduces the burden on derived classes that want to override the virtual function. 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. -- Doug Harrison Visual C++ MVP
From: Giovanni Dicanio on 4 May 2008 18:51
"Doug Harrison [MVP]" <dsh(a)mvps.org> ha scritto nel messaggio news:skcs141g2ubheru27v65vue1a60st8uqjh(a)4ax.com... > CString Get( > CString strURL, > const CString& strErrorMsg = CString()) > { > return Get_impl(strURL, strErrorMsg); > } > > virtual CString Get_impl( > const CString& strURL, > const CString& strErrorMsg); > [...] avoid overloading virtual > functions, which reduces the burden on derived classes that want to > override the virtual function. I like that! Thanks for sharing, Giovanni |