std::stringstream returning back values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TBass

    std::stringstream returning back values

    Hi.

    I wrote a "tag" class to store values. The user gets to store most any
    type he would need. Instead of getting too complicatated, I decided I
    would store the value as a stringstream, then overload the SetValue
    method.

    ....

    protected:
    std::stringstre am m_szValue;

    ....


    void
    CFusionTag::Set Value( char *szValue )
    {
    m_szValue << szValue;

    MessageBox::Sho w( m_szValue.str() .c_str() );

    } /* ::SetValue */


    void
    CFusionTag::Set Value( unsigned int uiValue )
    {
    m_szValue << uiValue;
    }

    void
    CFusionTag::Set Value( int iValue )
    {
    m_szValue << iValue;
    }

    void
    CFusionTag::Set Value( float fValue )
    {
    m_szValue << fValue;
    }

    void
    CFusionTag::Set Value( double dValue )
    {
    m_szValue << dValue;
    }

    void
    CFusionTag::Set Value( long lValue )
    {
    m_szValue << lValue;
    }


    That works fine (I'm aware of the obvious flaw in this code, but I use
    taking advantage of that for debugging purposes). The value gets
    stored.

    My problem (which is probably obvious to everyone but me) is returning
    the value when someone calls the GetValue method.

    void
    CFusionTag::Get Value( char *szValue )
    {

    MessageBox::Sho w( "Sending back a char value!" );
    szValue = (char *)m_szValue.str ().c_str();
    }

    The char string returned is always blank. I thought this would be
    pretty simple, but STL keeps getting the better of me. Can anyone
    point me in the right direction?

    Thanks in advance,
    T
  • TBass

    #2
    Re: std::stringstre am returning back values

    I don't know if this would be the "C++" way, but doing it the way I
    would do it in C worked.

    void
    CFusionTag::Get Value( char *szValue )
    {
    memcpy( szValue, m_szValue.str() .c_str(), m_szValue.str() .length() );
    }

    Comment

    • Daniel T.

      #3
      Re: std::stringstre am returning back values

      In article
      <32f67c60-fcdf-4fb6-8974-dd0d59fc2103@e4 g2000hsg.google groups.com>,
      TBass <tbj@automatedd esign.comwrote:
      Hi.
      >
      I wrote a "tag" class to store values. The user gets to store most any
      type he would need. Instead of getting too complicatated, I decided I
      would store the value as a stringstream, then overload the SetValue
      method.
      >
      ...
      >
      protected:
      std::stringstre am m_szValue;
      >
      ...
      >
      >
      void
      CFusionTag::Set Value( char *szValue )
      {
      m_szValue << szValue;
      >
      MessageBox::Sho w( m_szValue.str() .c_str() );
      >
      } /* ::SetValue */
      >
      >
      void
      CFusionTag::Set Value( unsigned int uiValue )
      {
      m_szValue << uiValue;
      }
      >
      void
      CFusionTag::Set Value( int iValue )
      {
      m_szValue << iValue;
      }
      >
      void
      CFusionTag::Set Value( float fValue )
      {
      m_szValue << fValue;
      }
      >
      void
      CFusionTag::Set Value( double dValue )
      {
      m_szValue << dValue;
      }
      >
      void
      CFusionTag::Set Value( long lValue )
      {
      m_szValue << lValue;
      }
      >
      >
      That works fine (I'm aware of the obvious flaw in this code, but I use
      taking advantage of that for debugging purposes). The value gets
      stored.
      >
      My problem (which is probably obvious to everyone but me) is returning
      the value when someone calls the GetValue method.
      >
      void
      CFusionTag::Get Value( char *szValue )
      {
      >
      MessageBox::Sho w( "Sending back a char value!" );
      szValue = (char *)m_szValue.str ().c_str();
      }
      >
      The char string returned is always blank.
      Your not returning anything. Try this:

      std::string
      CFusionTag::Get Value() const
      {
      return m_szValue.str() ;
      }
      I thought this would be pretty simple, but STL keeps getting the
      better of me. Can anyone point me in the right direction?
      Your having problems because you are trying to use C idioms (char*)
      instead of C++ idioms (string.)

      Comment

      • zhangyw80@yahoo.com.cn

        #4
        Re: std::stringstre am returning back values

        On 1ÔÂ9ÈÕ, ÉÏÎç10ʱ16·Ö, TBass <t...@automated design..comwrot e:
        I don't know if this would be the "C++" way, but doing it the way I
        would do it in C worked.
        >
        void
        CFusionTag::Get Value( char *szValue )
        {
        memcpy( szValue, m_szValue.str() .c_str(), m_szValue.str() .length());
        >
        >
        >
        }- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
        >
        - ÏÔʾÒý
        the folowing is another C way through passing argument by pointer:
        void
        CFusionTag::Get Value(const char** szValue)
        {
        if(szValue != NULL)
        {
        *szValue = m_szValue.str() .c_str();
        }
        }





        Comment

        • acehreli@gmail.com

          #5
          Re: std::stringstre am returning back values

          On Jan 8, 10:36 pm, zhangy...@yahoo .com.cn wrote:
          On 1ÔÂ9ÈÕ, ÉÏÎç10ʱ16·Ö, TBass <t...@automated design.comwrote :I don't know if this would be the "C++" way, but doing it the way I
          would do it in C worked.
          >
          void
          CFusionTag::Get Value( char *szValue )
          {
          memcpy( szValue, m_szValue.str() .c_str(), m_szValue.str() .length() );
          Hmmm... Do we have enough space at szValue? Probably not! :(
          the folowing is another C way through passing argument by pointer:
          void
          CFusionTag::Get Value(const char** szValue)
          {
          if(szValue != NULL)
          {
          *szValue = m_szValue.str() .c_str();
          That doesn't work... The std::string value that str() returns is a
          temporary object that dies at the semicolon. So, the C-style string
          that is returned by c_str() is not valid beyond that point.

          Ali

          Comment

          • TBass

            #6
            Re: std::stringstre am returning back values

            That doesn't work... The std::string value that str() returns is a
            temporary object that dies at the semicolon. So, the C-style string
            that is returned by c_str() is not valid beyond that point.
            >

            Ah ha! That's been my problem. I did not know that it was temporary.
            Thanks!


            [snip]
            I thought this would be pretty simple, but STL keeps getting the
            better of me. Can anyone point me in the right direction?
            Your having problems because you are trying to use C idioms (char*)
            instead of C++ idioms (string.)
            [/snip]

            The reason for the char * as an argument was because it's actually a
            vector that I'm passing into the structure.

            std::vector<cha rmyvector(500);
            mytag->GetValue( &myvector[0] );

            Comment

            • Daniel T.

              #7
              Re: std::stringstre am returning back values

              TBass <tbj@automatedd esign.comwrote:
              That doesn't work... The std::string value that str() returns is a
              temporary object that dies at the semicolon. So, the C-style string
              that is returned by c_str() is not valid beyond that point.
              >
              >
              Ah ha! That's been my problem. I did not know that it was temporary.
              Thanks!
              >
              >
              [snip]
              I thought this would be pretty simple, but STL keeps getting the
              better of me. Can anyone point me in the right direction?
              >
              Your having problems because you are trying to use C idioms (char*)
              instead of C++ idioms (string.)
              [/snip]
              >
              The reason for the char * as an argument was because it's actually a
              vector that I'm passing into the structure.
              >
              std::vector<cha rmyvector(500);
              mytag->GetValue( &myvector[0] );
              Creating a buffer and assuming it is big enough (or making it insanely
              huge) is a (rather poor) C idiom. Better would be:


              string str = mytag->GetValue();
              std::vector<cha rmyvector( str.begin(), str.end() );

              Even having the function fill a vector that is provided would be better
              than using the raw char*.

              std::vector<cha rmyvector;
              mytag->GetValue( back_inserter( myvector ) );

              Comment

              • Daniel T.

                #8
                Re: std::stringstre am returning back values

                In article
                <685c8a02-58e0-4ea5-8e64-0907bbcc62b1@j7 8g2000hsd.googl egroups.com>,
                TBass <tbj@automatedd esign.comwrote:
                I don't know if this would be the "C++" way, but doing it the way I
                would do it in C worked.
                >
                void
                CFusionTag::Get Value( char *szValue )
                {
                memcpy( szValue, m_szValue.str() .c_str(), m_szValue.str() .length() );
                }
                What if the block passed in isn't big enough? At the very least, even in
                C, you should pass in the size of the block handed over...

                void
                CFusionTag::Get Value( char* value, int value_size ) {
                memcpy( value, m_szValue.str() .c_str(),
                std::min( value_size, m_szValue.str() .length() ) );
                }

                The fact that you are having to chain the calls (like
                "m_szValue.str( ).c_str()" and "m_szValue.str( ).length()") means there is
                probably an easier way.

                Comment

                Working...