string copy problem

This is a discussion on string copy problem within the C++ Programming forums, part of the General Programming Boards category; Code: void CString::Copy( const CString& string ) { if( this != &string ) { if( string.IsValid() ) { const TCHAR* ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    string copy problem

    Code:
    void CString::Copy( const CString& string )
    {
        if( this != &string )
        {
            if( string.IsValid() )
            {
                const TCHAR* data   = NULL;
                int len             = string.Length();
    
                // this is probably unnecessary
                if( m_Data != NULL )
                {
                    delete [] m_Data;
                    m_Data      = NULL;
    
                    m_Length    = 0;
                    m_Size      = 0;
                    m_isValid   = false;
                }
    
                m_Data  = new TCHAR[len + 1 ];
                data    = string.C_Str();
    
                for( int index = 0; index < len; ++index )
                    m_Data[index] = data[index];
    
                m_Length            = len;
                m_Size              = CalcSizeInBytes();
    
                m_Data[m_Length]    = '0';
                m_isValid           = true;
            }
        }
    }
    Problem:
    Code:
    CString string( _T( "Hello World" ) );
        CString me;
    
        me = _T( "Ervin Ashley" );
        me = string;
    comes out as:
    string = Hello World
    me = Hello World0

    but I cant find anything wrong with my code.
    My other copy function works just fine.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    m_Data[m_Length]    = '0';
    Shouldn't this be '\0'?

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Quote Originally Posted by tabstop View Post
    Code:
    m_Data[m_Length]    = '0';
    Shouldn't this be '\0'?
    Indeed, that fixed it.

    I cant believe I missed that.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 03:29 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. C/C++ String Problem.
    By Jaken Veina in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2005, 11:11 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21