-
August 3rd, 2015, 09:32 AM
#1
Pass reference to string as default parameter
I have legacy code where the function takes five arguements. I want to put the last arguement as the default arguement (this is because i donot want to change the
legacy code everywhere. But the issue is the last arguement is of string type and for some reason it needs to be passed as reference. Not sure how i can pass a default arguement
string as a reference
bool FunctionName(
Atype * pCmd, const BType & stBInfo, const CType & stCType, const u8 nNum, XNameSpace::YNameSpace::ZRequestPtr req = XNameSpace::YNameSpace::IRequestPtr(),
string sClassName = "");
The issue is if i pass the last arguement sClassName by value , somehow the string is appearing as NULL. If i pass as reference, i am getting compile error..
Is there anyway i can pass as reference ?
In our codebase, the string is typedef as
typedef basic_string<char> string;
Last edited by pdk5; August 3rd, 2015 at 09:50 AM.
-
August 3rd, 2015, 10:03 AM
#2
Re: Pass reference to string as default parameter
You could consider overloading the function.
-
August 3rd, 2015, 10:04 AM
#3
Re: Pass reference to string as default parameter
For c++11, this works with MSVS 2015
Code:
typedef basic_string<char> bstring;
void byref(bstring& bst = bstring("default"))
{
cout << bst << endl;
bst = "qwerty";
}
int main()
{
bstring st = "123";
byref();
byref(st);
cout << st << endl;
return 0;
}
However, the issue is why passing by value isn't working.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 3rd, 2015, 10:08 AM
#4
Re: Pass reference to string as default parameter
@laseligt Thanks a lot for the help. I will try this.
Last edited by pdk5; August 3rd, 2015 at 10:11 AM.
-
August 3rd, 2015, 10:10 AM
#5
Re: Pass reference to string as default parameter
Originally Posted by 2kaud
For c++11, this works with MSVS 2015
It is an extension to C++ that existed even before C++11, and it is not standard C++ even after C++11 (we have rvalue references by a slightly different syntax instead).
-
August 3rd, 2015, 10:12 AM
#6
Re: Pass reference to string as default parameter
@kaud: yes not sure why passing by value isnt working for me, may be something to do with the copy constructor ..i am not a c++ expert thought..havent dig deeper on this..
-
August 3rd, 2015, 10:17 AM
#7
Re: Pass reference to string as default parameter
Originally Posted by pdk5
The issue is if i pass the last arguement sClassName by value , somehow the string is appearing as NULL.
Is it appearing as an empty string (not NULL: that is a null pointer constant) inside the function or in the caller after you have called the function. For the latter, the obvious reason is that by passing by value, the function only changes a copy of the string, so the string in the caller remains unchanged. For the former... well, that should not be happening.
-
August 3rd, 2015, 10:20 AM
#8
Re: Pass reference to string as default parameter
@laserlight: I can see the string in the calling function being properly passed by the debugger. But it is appearing as "empty string" in the called function
-
August 3rd, 2015, 10:24 AM
#9
Re: Pass reference to string as default parameter
@laserlight: Overloading may be option, but creates lot of duplicate code
-
August 3rd, 2015, 10:24 AM
#10
Re: Pass reference to string as default parameter
Temporarily change the default argument to "default argument". Do you still see an empty string in the called function?
Originally Posted by pdk5
Overloading may be option, but creates lot of duplicate code
No, it doesn't. The overloaded function can forward to your modified function. But if you can pass by value, then you should not be passing by non-const lvalue reference. Perhaps you should be passing by const lvalue reference.
-
August 4th, 2015, 02:58 AM
#11
Re: Pass reference to string as default parameter
Sorry for the delay in response, had some work yesterday.
@laserlight: tried the solution you mentioned, but getting compile error. ..sorry for my naiveness in c++
-
August 4th, 2015, 03:05 AM
#12
Re: Pass reference to string as default parameter
I tried to compile the small program kaud has given earlier, with g++, but getting the error:
sing.cpp:9: error: default argument for 'bstring& bst' has type 'bstring'
-
August 4th, 2015, 05:49 AM
#13
Re: Pass reference to string as default parameter
Sorry it was my mistake in the earlier code
Passing the parameter as default arguement (aftet little fiddling) worked..
Thanks a lot laserlight and kaud for the hints and help
-
August 4th, 2015, 08:15 AM
#14
Re: Pass reference to string as default parameter
Originally Posted by pdk5
Passing the parameter as default arguement (aftet little fiddling) worked..
Thanks a lot laserlight and kaud for the hints and help
You're welcome. What solution did you settle on in the end?
-
August 4th, 2015, 08:27 AM
#15
Re: Pass reference to string as default parameter
By passing as the default argument .
Overloading might have worked , but didnot try that yet (may be when i have sometime, i will explore these options )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|