+ Reply to Thread
Results 1 to 2 of 2

Thread: What should I use, assignment or initialization list in constructor?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    37

    What should I use, assignment or initialization list in constructor?

    What should I use, assignment or initialization list in constructor?

    Is there any advantage of one scheme over another?

    Please provide some example.

  2. #2
    1. Using initialization list:

    Code:
    class CInteger
    {
    private:
    	int m_nValue ;
    public:
    	CInteger()
    	{
    		m_nValue = -1 ;
    		OutputDebugString("Executing CInteger::CInteger default constructor\n") ;
    	}
    
    	CInteger(int nValue)
    	{
    		m_nValue = nValue ;
    
    		OutputDebugString("Executing CInteger::CInteger constructor\n") ;
    	}
    
    	CInteger(const CInteger& objInteger)
    	{
    		m_nValue = objInteger.m_nValue ;
    
    		OutputDebugString("Executing CInteger::CInteger copy constructor\n") ;
    	}
    
    	CInteger& operator =(const CInteger& objInteger)
    	{
    		if (this != &objInteger)
    		{
    			m_nValue = objInteger.m_nValue ;
    
    			OutputDebugString("Executing CInteger::CInteger assignment operator\n") ;
    		}
    
    		return *this ;
    	}
    
    	~CInteger()
    	{
    		OutputDebugString("Executing CInteger::CInteger destructor\n") ;
    	}
    } ;
    
    class CBase1
    {
    	CInteger m_objInteger ;
    public:
    	
    	CBase1(const CInteger& objInteger) : m_objInteger(objInteger)
    	{
    		OutputDebugString("Executing CBase1::CBase1 constructor\n") ;
    	}
    
    	~CBase1()
    	{
    		OutputDebugString("Executing CBase1::CBase1 destructor\n") ;
    	}
    } ;
    
    void UseInitList()
    {
    	CInteger objInteger ;
    
    	CBase1 objBase1(objInteger) ;
    }
    Output:
    Executing CInteger::CInteger default constructor
    Executing CInteger::CInteger copy constructor
    Executing CBase1::CBase1 constructor
    Executing CBase1::CBase1 destructor
    Executing CInteger::CInteger destructor
    Executing CInteger::CInteger destructor

    2. Using Assignment

    Code:
    class CInteger
    {
    private:
    	int m_nValue ;
    public:
    	CInteger()
    	{
    		m_nValue = -1 ;
    		OutputDebugString("Executing CInteger::CInteger default constructor\n") ;
    	}
    
    	CInteger(int nValue)
    	{
    		m_nValue = nValue ;
    
    		OutputDebugString("Executing CInteger::CInteger constructor\n") ;
    	}
    
    	CInteger(const CInteger& objInteger)
    	{
    		m_nValue = objInteger.m_nValue ;
    
    		OutputDebugString("Executing CInteger::CInteger copy constructor\n") ;
    	}
    
    	CInteger& operator =(const CInteger& objInteger)
    	{
    		if (this != &objInteger)
    		{
    			m_nValue = objInteger.m_nValue ;
    
    			OutputDebugString("Executing CInteger::CInteger assignment operator\n") ;
    		}
    
    		return *this ;
    	}
    
    	~CInteger()
    	{
    		OutputDebugString("Executing CInteger::CInteger destructor\n") ;
    	}
    } ;
    
    class CBase1
    {
    	CInteger m_objInteger ;
    public:
    	
    	CBase1(const CInteger& objInteger)// : m_objInteger(objInteger)
    	{
    		m_objInteger = objInteger ;
    		OutputDebugString("Executing CBase1::CBase1 constructor\n") ;
    	}
    
    	~CBase1()
    	{
    		OutputDebugString("Executing CBase1::CBase1 destructor\n") ;
    	}
    } ;
    
    void UseAssignment()
    {
    	CInteger objInteger ;
    
    	CBase1 objBase1(objInteger) ;
    }
    Output:

    Executing CInteger::CInteger default constructor
    Executing CInteger::CInteger default constructor
    Executing CInteger::CInteger assignment operator
    Executing CBase1::CBase1 constructor
    Executing CBase1::CBase1 destructor
    Executing CInteger::CInteger destructor
    Executing CInteger::CInteger destructor


    Summary
    Assignment takes two steps while initialization list just calls Copy constructor!

+ 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