std::ostringstream to get CString::GetBufferSetLength()-like functionality?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ibox73
    New Member
    • Feb 2012
    • 6

    #1

    std::ostringstream to get CString::GetBufferSetLength()-like functionality?

    Hi there,
    Can std::ostringstr eam be used to get CString::GetBuf ferSetLength()-like functionality? CString provides rather convenient way to use it as a buffer, in particular for winapi functions.

    Code:
    //just an illustration
    CString str;
    SHGetSpecialFolderPath( 0, str.GetBufferSetLength(..
    return str;
    Is it possible to do such a trick using std::ostringstr eam? // wostringstream, to be more accurate
    Thanks in advance
    Last edited by ibox73; Feb 9 '12, 11:33 AM. Reason: Wording
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I would use an STL vector as a buffer since it is implemented as a array and you have access to the array. Then vector::capacit y would get you the current buffer length.

    Comment

    • ibox73
      New Member
      • Feb 2012
      • 6

      #3
      Thank you for your reply. The whole CString trick is that you can use say the stack string variable as a buffer and then return it, so no copying is fulfilled. Additional buffer can be declared as char[] equally well, but the problem is the ownership for it cannot be delegated to the return result AFAIK.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        To avoid unnecessary copies use a reference.

        Code:
        vector<int> arr;
        
        funct(arr);
        where

        Code:
        void funct(vector<int>& data)
        {
        
        }
        Inside the function funct, data is the same variable as arr. data is just an alias for arr. No copy is made on the call.

        Comment

        • ibox73
          New Member
          • Feb 2012
          • 6

          #5
          The function I have to write must return string, not an array, so unfortunately this doesn't suit.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            So have the fuction return a string. In that case you have a vector<char> so you can use that as an argument to the string constructor:

            Code:
            string funct(vector<char>& data)
            {
                 //some processing
                 return string(&data[0], data.size());
            }

            Comment

            • ibox73
              New Member
              • Feb 2012
              • 6

              #7
              [ -- server error -- ]

              Comment

              • ibox73
                New Member
                • Feb 2012
                • 6

                #8
                [ -- server error -- ]
                Last edited by ibox73; Jun 28 '12, 09:27 PM. Reason: My message was duplicated on submit for some mystical reasons)))

                Comment

                • ibox73
                  New Member
                  • Feb 2012
                  • 6

                  #9
                  Even I haven't found a std-based replacement for the old sweet CStrings's getbuffersetlen gth, - thank you very much, weaknessforcats , for your replies!

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    getbuffersetlen gth is weakness of CString because it exposes the underlying implementation. When you use it you tie your application to that implementation forever.

                    Breaking your app while redesigning CString is a big no-no. Therefore CString is now frozen as far as you are concerned. The safe thing to do now is to embed the CString code in your app so you have control over the implementation you used to build the app.

                    Comment

                    Working...