+ Reply to Thread
Results 1 to 2 of 2

Thread: What is std::move method? When should I use it in my C++ program?

  1. #1

    What is std::move method? When should I use it in my C++ program?

    I just came to know about std::move method. This is new in C++ 11. What this method is all about? When can I use it in my program? Please provide some nice example.

  2. #2
    You can use std::move is for move semantic. You can use it to move or transfer content of argument to some other object.

    Below example show usefulness of std::move:

    Code:
    class CMyObject
    {
    private:
    	int m_nDataLength ;
    	unsigned char* m_pdata ;
    public:
    	CMyObject()
    	{
    		m_pdata = nullptr ;
    		m_nDataLength = 0 ;
    	}
    
    	CMyObject(int nDataLength, unsigned char *pdata)
    	{
    		if (NULL != pdata)
    		{
    			m_pdata = new unsigned char[nDataLength] ;
    			memcpy(m_pdata, pdata, nDataLength) ;
    			m_nDataLength = nDataLength ;
    		}
    	}
    		
    	CMyObject(CMyObject&& objMyObject) : m_nDataLength(0), m_pdata(nullptr)
    	{
    		*this = std::move(objMyObject) ;
    	}
    
    	CMyObject& operator=(CMyObject&& objMyObject)
    	{
    		if (this != &objMyObject)
    		{
    			delete [] m_pdata ;
    			m_pdata = nullptr ;
    			m_nDataLength = 0 ;
    
    			if (NULL != objMyObject.m_pdata)
    			{
    				m_pdata = new unsigned char[objMyObject.m_nDataLength] ;
    				memcpy(m_pdata, objMyObject.m_pdata, objMyObject.m_nDataLength) ;
    				m_nDataLength = objMyObject.m_nDataLength ;
    
    				objMyObject.m_pdata = nullptr ;
    				objMyObject.m_nDataLength = 0 ;
    			}
    		}
    
    		return *this ;
    	}
    	
    	~CMyObject()
    	{
    		if (NULL != m_pdata)
    		{
    			delete []m_pdata ;
    			m_pdata = nullptr ;
    		}
    		m_nDataLength = 0 ;
    	}
    } ;

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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