+ Reply to Thread
Results 1 to 2 of 2

Thread: What is move constructor and how to use it?

  1. #1
    Senior Member
    Join Date
    Feb 2012
    Posts
    109

    What is move constructor and how to use it?

    There is new feature in C++ 11 and that is move constructor. How can I define and declared it? How to use it in my program. Please provide some example.

  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    29
    move constructor can be defined as using && operator.

    EXample:
    Code:
    class CEmployee
    {
    private:
    	char* m_pName ;
    public:
    	CEmployee(char *pName)
    	{
    		if (NULL != pName)
    		{
    			m_pName = new char[strlen(pName) + 1] ;
    			strcpy(m_pName, pName) ;
    		}
    	}
    
    	CEmployee(CEmployee&& objEmployee)
    	{
    		if (NULL != objEmployee.m_pName)
    		{
    			m_pName = new char[strlen(objEmployee.m_pName) + 1] ;
    			strcpy(m_pName, objEmployee.m_pName) ;
    
    			objEmployee.m_pName = NULL ;
    		}
    	}
    
    	~CEmployee()
    	{
    		if (NULL != m_pName)
    		{
    			delete []m_pName ;
    			m_pName = NULL ;
    		}
    	}
    } ;
    
    CEmployee GetEmployee()
    {
    	CEmployee objEmployee("Roger") ;
    	return objEmployee ;
    }
    void CMyDlg::OnBnClickedOk()
    {
    	CEmployee objMyObject1 = GetEmployee() ;
    }

+ 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