CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    May 2015
    Posts
    500

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pass reference to string as default parameter

    You could consider overloading the function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  4. #4
    Join Date
    May 2015
    Posts
    500

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pass reference to string as default parameter

    Quote 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).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    May 2015
    Posts
    500

    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..

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pass reference to string as default parameter

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    May 2015
    Posts
    500

    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

  9. #9
    Join Date
    May 2015
    Posts
    500

    Re: Pass reference to string as default parameter

    @laserlight: Overloading may be option, but creates lot of duplicate code

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    May 2015
    Posts
    500

    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++

  12. #12
    Join Date
    May 2015
    Posts
    500

    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'

  13. #13
    Join Date
    May 2015
    Posts
    500

    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

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Pass reference to string as default parameter

    Quote 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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    May 2015
    Posts
    500

    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 )

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured