+ Reply to Thread
Results 1 to 2 of 2

Thread: Simple use of shared_ptr

  1. #1

    Simple use of shared_ptr

    Hi Everyone, I am newbie to C++ world and I am learning new techniques. I would like to get an example of using shear_ptr. Please suggest me how to use it. Thanks.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    38
    shared_ptr is:
    a. smart pointer
    b. It hold the object that you create
    c. It maintains reference counts
    d. It de-alloctaes the object when reference count reaches to 0.

    Example:
    Code:
    class CValue
    {
    private:
    	int m_nValue ;
    public:
    	CValue(int nValue)
    	{
    		OutputDebugString("Constructor called!\n") ;
    		m_nValue = nValue ;
    	}
    
    	~CValue()
    	{
    		OutputDebugString("Destructor called!\n") ;
    	}
    
    	int GetValue()
    	{
    		return m_nValue ;
    	}
    } ;
    
    void SimpleSharedPtr()
    {
    	std::shared_ptr<CValue> sp(new CValue(10)) ;
    	int nValue = sp->GetValue() ;
    }

+ 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