+ Reply to Thread
Results 1 to 2 of 2

Thread: Difference between pass-by-reference and pass-by-value?

  1. #1

    Difference between pass-by-reference and pass-by-value?

    Difference between pass-by-reference and pass-by-value?

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Code:
    class CStudent
    {
    private:
    	int m_nRoleID ;
    	char *m_pName ;
    
    public:
    	CStudent(int nRoleID, char *pName)
    	{
    		m_nRoleID = nRoleID ;
    		m_pName = new char[100] ;
    		strcpy(m_pName, pName) ;
    	}
    
    	CStudent(const CStudent& objStudent)
    	{
    		m_nRoleID = objStudent.m_nRoleID ;
    		m_pName = new char[100] ;
    		strcpy(m_pName, objStudent.m_pName) ;
    	}
    
    	~CStudent()
    	{
    		if (NULL != m_pName)
    		{
    			delete m_pName ;
    			m_pName = NULL ;
    		}
    	}
    } ;
    
    void GiveMeReference(const CStudent &objStudent)
    {
    	//use student
    }
    
    void GiveMeValue(CStudent objStudent)
    {
    	//use student
    }
    
    void UsePassByReference()
    {
    	CStudent objStudent(1, "Maxi") ;
    	GiveMeReference(objStudent) ;
    	GiveMeValue(objStudent) ; //this call's class copy contructor! Hence one call to create a new object!
    }
    Note:
    As per above example, pass-by-value is costly than pas-by-reference.

+ Reply to 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